调用第三方api接口,别人返回的是JSONString,所以我们自己拿到还要重新转义成JSON,但是jsonString可能中属性会有嵌套json数组或者json对象。所以写了一个工具类来转化。
效果图:
原JsonString
1.
2.
转义后
1.
2.
引用fastjson包来做转义
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.41</version>
</dependency>
转换工具类:JsonUtils
package com.szmsd.hulutianxia.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;
import java.util.Set;
/**
* @author TuanTuan
* @version 1.0
* @date 2022/5/25 15:02
*/
@Slf4j
public class JsonUtils {
/**
* 递归处理jsonString转换json对象
* JSONString中可能嵌套多层都是JSONString格式,
* 不是标准的JSON格式,前端不能直接使用
* @param json jsonString字符串
* @return
*/
public static Object jsonStrToJSonRecursion(String json){
if (json==null) {
return null;
}
try {
JSONObject jsonObject = JSONObject.parseObject(json);
Object object = null;
Set<Map.Entry<String, Object>> entries = jsonObject.entrySet();
for (Map.Entry<String, Object> entry: entries) {
String key = entry.getKey();
Object value = entry.getValue();
if (value == null) {
continue;
}
if (jsonObject.getString(key).startsWith("[")) {
//属性值是json数组
JSONArray jsonArray = JSONArray.parseArray(entry.getValue().toString());
JSONArray copyArray = new JSONArray();
for (int i =0 ,len = jsonArray.size();i<len ;i++) {
JSONObject indexArray = jsonArray.getJSONObject(i);
Object o = jsonStrToJSonRecursion(indexArray.toString());
copyArray.add(o);
}
object = copyArray;
} else if (jsonObject.getString(key).startsWith("{")) {
//属性值是json对象
JSONObject jsonObj = JSONObject.parseObject( entry.getValue().toString());
//递归处理下面是否有json或者jsonArray
object = jsonStrToJSonRecursion( jsonObj.toString());
}else{
continue;
}
jsonObject.put(key, object);
}
return jsonObject;
}catch (Exception e){
log.error("错误入参不为json格式 error:"+e.getMessage());
}
return null;
}
public static void main(String[] args) {
String json3 = "{\"tradeType\":\"ALI\",\"needBalance\":\"N\",\"thirdOrderNo\":\"202022051222001464051403221274\",\"reqMsgId\":\"419cea41-5dbf-4ee9-a965-b8763cf9fc70\",\"type\":\"S\",\"orderInfo\":\"订单描述999\",\"amount\":\"1\",\"paymentAmount\":\"0\",\"transIndex\":\"100110722051229943832\",\"userInfoType\":\"ALI\",\"couponInfo\":\"[{\\\"amount\\\":\\\"0.01\\\",\\\"merchantContribute\\\":\\\"0.00\\\",\\\"name\\\":\\\"实体店付款通用立减券\\\",\\\"otherContribute\\\":\\\"0.01\\\",\\\"type\\\":\\\"ALIPAY_CASH_VOUCHER\\\",\\\"voucherId\\\":\\\"2022051000073002056409QB60D3\\\",\\\"data\\\":\\\"{\\\\\\\"a\\\\\\\":\\\\\\\"1\\\\\\\",\\\\\\\"b\\\\\\\":\\\\\\\"2\\\\\\\"}\\\"},{\\\"amount\\\":\\\"0.01\\\",\\\"merchantContribute\\\":\\\"0.00\\\",\\\"name\\\":\\\"实体店付款通用立减券\\\",\\\"otherContribute\\\":\\\"0.01\\\",\\\"type\\\":\\\"ALIPAY_CASH_VOUCHER\\\",\\\"voucherId\\\":\\\"2022051000073002056409QB60D3\\\",\\\"data\\\":\\\"{\\\\\\\"a\\\\\\\":\\\\\\\"3\\\\\\\",\\\\\\\"b\\\\\\\":\\\\\\\"4\\\\\\\"}\\\"} ]\",\"balanceDay\":\"20220512\",\"signature\":\"Sq8Ysqv2rsCVA3EoNPMfhDu62Q1KAUasW9dIJL6XBfaLep7rmtRjK3MRjnXaZHb0vYDGSrlCdU1zS8V0jZZ7hAIaFJ7BF7I3wafX8AwQXiegqdkedYat9S3wND82pNqXnJJXtPp140kK3Tnm1Ok8QNFEHlPpjm3tLQCJyQdH5I8=\",\"merNo\":\"M000018491\",\"payCompleteTime\":\"20220512174819\",\"tradeStatus\":\"S\",\"orderType\":\"BUSINESS_ORDER\",\"tradeTime\":\"20220512174720\",\"subTradeType\":\"QRCODE\",\"discountsAmount\":\"1\",\"chargeRate\":\"0.002\",\"code\":\"0000\",\"platOrderNo\":\"202205122130314838OR011551331933\",\"charge\":\"0\",\"orderNo\":\"HULUPOS20220512174721P31665386927\",\"deviceNo\":\"888888\",\"channelOrderNo\":\"202205122130314838OR011551331933\",\"subTradeStatus\":\"COMPLETE\",\"merName\":\"八戒烤猪蹄\",\"bankType\":\"OTHER\",\"settleOrderAmount\":\"1\",\"payType\":\"OTHER\",\"payerInfo\":\"{\\\"userId\\\":\\\"2088912766264050\\\",\\\"buyerLogonId\\\":\\\"199****0204\\\",\\\"fundBillList\\\":\\\"[{\\\\\\\"amount\\\\\\\":\\\\\\\"0.01\\\\\\\",\\\\\\\"fundChannel\\\\\\\":\\\\\\\"DISCOUNT\\\\\\\",\\\\\\\"dataList\\\\\\\":\\\\\\\"[{\\\\\\\\\\\\\\\"A\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"1\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"B\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"2\\\\\\\\\\\\\\\"}]\\\\\\\"}]\\\"}\"}";
Object o = jsonStrToJSonRecursion(json3);
System.out.println(o);
}
}