拼接json格式post

将json格式的字符拼接成需要的url post请求

1.json格式的字符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"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对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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);
}