拼接json格式成post请求URL

2017-8-19 / 0评 / Java

说明:将json格式的字符拼接成需要的url post请求
### 1.json格式的字符
```bash
{
  "notifyStr": {
    "charset": "utf-8",
    "biz_content": "{\"mer_id\":\"02360\",\"bank_disc_amt\":0,\"mer_disc_amt\":0,\"pay_time\":\"20170901\",\"attach\":\"\",\"cust_id\":\"GC75JOxg3UFjG\",\"order_id\":\"0209010780760\",\"out_trade_no\":\"P1708181ec800000001\",\"total_amt\":1,\"coupon_amt\":0,\"payment_amt\":1,\"card_no\":\"622202*********9149\",\"return_msg\":\"success\",\"ecoupon_amt\":0,\"total_disc_amt\":0,\"msg_id\":\"020099210780760\",\"point_amt\":0,\"return_code\":\"0\"}",
    "format": "json",
    "sign": "JdjqDJD71rur7a48e/conMlG0lY6sryvTmLDe9tA0EyxjpcspnvBhsafUTtTkdQ6fZ1Qqj2K+75ELFfCVmhdydeTLpXX27KmTNBRtO5euLLZEJjFx7XWT6UWK3kHlTcFbkAl+smNRfsfplg=",
    "from": "hou-api",
    "api": "/ui/thirdparty/order/V1/pay",
    "app_id": "100000036501",
    "sign_type": "RSA",
    "timestamp": "2017-08-18 18:48:14"
  },
  "serial": "P170ec800000001"
}
```

### 2.将json转换为Java对象
```bash
public class NotifyReqDTO implements Serializable {
        private static final long serialVersionUID = -4715222968104483630L;

        /**
         * 流水号
         */
        private String serial;

        /**
         * 格式报文
         */
        private Map<String, String> notifyStr;

        public String getSerial() {
            return serial;
        }

        public void setSerial(String serial) {
            this.serial = serial;
        }

        public Map<String, String> getNotifyStr() {
            return notifyStr;
        }

        public void setNotifyStr(Map<String, String> notifyStr) {
            this.notifyStr = notifyStr;
        }
    }
```

### 3.拼接处理-java代码
```bash
package com.houxiurong.utils;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by houxiurong on 2017/8/14.
 */
public class SignUtils {

    private static Logger logger = LoggerFactory.getLogger(SignUtils.class);

/**
     * 签参数组装
     *
     * @param params
     * @return
     */
    public static String buildMapSignString(Map<String, String> params) {
        List<String> keys = new ArrayList<String>(params.size());
        for (String key : params.keySet()) {
            if ("sign".equals(key))
                continue;
            keys.add(key);
        }
        Collections.sort(keys);
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = params.get(key);
            if (i == keys.size() - 1) {
                buf.append(key + "=" + value);
            } else {
                buf.append(key + "=" + value + "&");
            }
        }
        return buf.toString();
    }

 }
```
### 3.将上面的json转换为java对象,使用alibaba.fastjaon的JSON工具
```bash
public void static main(String[] args){
  String strObject ="{\n" +
                "  \"notifyStr\": {\n" +
                "    \"charset\": \"utf-8\",\n" +
                "    \"biz_content\": \"{\\\"mer_id\\\":\\\"02360\\\",\\\"bank_disc_amt\\\":0,\\\"mer_disc_amt\\\":0,\\\"pay_time\\\":\\\"20170901\\\",\\\"attach\\\":\\\"\\\",\\\"cust_id\\\":\\\"GC75JOxg3UFjG\\\",\\\"order_id\\\":\\\"0209010780760\\\",\\\"out_trade_no\\\":\\\"P1708181ec800000001\\\",\\\"total_amt\\\":1,\\\"coupon_amt\\\":0,\\\"payment_amt\\\":1,\\\"card_no\\\":\\\"622202*********9149\\\",\\\"return_msg\\\":\\\"success\\\",\\\"ecoupon_amt\\\":0,\\\"total_disc_amt\\\":0,\\\"msg_id\\\":\\\"020099210780760\\\",\\\"point_amt\\\":0,\\\"return_code\\\":\\\"0\\\"}\",\n" +
                "    \"format\": \"json\",\n" +
                "    \"sign\": \"JdjqDJD71rur7a48e/conMlG0lY6sryvTmLDe9tA0EyxjpcspnvBhsafUTtTkdQ6fZ1Qqj2K+75ELFfCVmhdydeTLpXX27KmTNBRtO5euLLZEJjFx7XWT6UWK3kHlTcFbkAl+smNRfsfplg=\",\n" +
                "    \"from\": \"hou-api\",\n" +
                "    \"api\": \"/ui/thirdparty/order/V1/pay\",\n" +
                "    \"app_id\": \"100000036501\",\n" +
                "    \"sign_type\": \"RSA\",\n" +
                "    \"timestamp\": \"2017-08-18 18:48:14\"\n" +
                "  },\n" +
                "  \"serial\": \"P170ec800000001\"\n" +
                "}";
                
  NotifyReqDTO notifyReqDTO = JSON.parseObject(strObject, NotifyReqDTO.class);
    String formatStr = SignUtils.buildMapSignString(params);
    System.out.println("formatStr:" + formatStr);
    String formatResponseStr = "/houxiurong/notify/" + NotifyReqDTO.getSerial() + "?" + formatStr;
    String houSign = SignUtils.getSign(NotifyReqDTO.getNotifyStr());
    System.out.println("formatStr:" + formatStr);
}
```
原文来自:http://xrhou.github.io

本文共计 6826 字,感谢您的耐心浏览与评论。

声明:土豆丝不辣|版权所有,违者必究|如未注明,均为原创|转载请注明原文链接说明出处

0条回应:“拼接json格式成post请求URL”