怪事(json)年年有,今年何其多
{code=0000,
charset=UTF-8,
mId=51000001,
rDesc=,
cy=156,
sendTime=20200316061253,
cId=test-1,
tType=01,
pSt=02,
oNum=TEST_1584339173825,
pT=00,
orSeq=20031614120015152511,
omt=1}
方案一,处理字符串为全标准 json
public static void main(String[] args) {
String a = "{code=0000,charset=UTF-8, mId=51000001, rDesc=, cy=156, sendTime=20200316061253,cId=test-1, tType=01, pSt=02, oNum=TEST_1584339173825,pT=00, orSeq=20031614120015152511,omt=1}";
//方案一,处理字符串为全标准json
String b = a.replaceAll(" ","").replaceAll("=","\":\"");
b = b.replaceAll("\\{","{\"");
b = b.replaceAll("}","\"}");
b = b.replaceAll(",","\",\"");
JSONObject data = JSON.parseObject(b);
logger.info("b0:"+b);
logger.info("b1:"+data.toJSONString());
}
方案一,程序输出
b0:{"code":"0000","charset":"UTF-8","mId":"51000001","rDesc":"","cy":"156","sendTime":"20200316061253","cId":"test-1","tType":"01","pSt":"02","oNum":"TEST_1584339173825","pT":"00","orSeq":"20031614120015152511","omt":"1"}
b1:{"charset":"UTF-8","code":"0000","tType":"01","oNum":"TEST_1584339173825","pT":"00","rDesc":"","mId":"51000001","orSeq":"20031614120015152511","sendTime":"20200316061253","pSt":"02","cy":"156","omt":"1","cId":"test-1"}
方案二,处理字符串为半标准 json+Gson
public static void main(String[] args) {
//方案二,处理字符串为半标准json+Gson
String aa = "{code=0000,charset=UTF-8, mId=51000001, rDesc=, cy=156, sendTime=20200316061253,cId=test-1, tType=01, pSt=02, oNum=TEST_1584339173825,pT=00, orSeq=20031614120015152511,omt=1}";
String bb = aa.replaceAll("=",":").replaceAll(":,",":\"\",");
logger.info("bb0:"+bb);
Gson g = new Gson();
JsonObject obj = g.fromJson(bb, JsonObject.class);
logger.info("bb1:"+obj.toString());
}
方案二,程序输出
bb0:{code:0000,charset:UTF-8, mId:51000001, rDesc:"", cy:156, sendTime:20200316061253,cId:test-1, tType:01, pSt:02, oNum:TEST_1584339173825,pT:00, orSeq:20031614120015152511,omt:1}
bb1:{"code":"0000","charset":"UTF-8","mId":51000001,"rDesc":"","cy":156,"sendTime":20200316061253,"cId":"test-1","tType":"01","pSt":"02","oNum":"TEST_1584339173825","pT":"00","orSeq":20031614120015152511,"omt":1}
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于