Java基础之《fastJson使用》

1、String转换为JsonObject
JSONObject resJson = JSONObject.parseObject(response);

2、获取嵌套的Json对象
JSONObject alipayResponse = resJson.getJSONObject("alipay_trade_precreate_response");

3、获取Json对象key对应的value
String sign = resJson.getString("sign");

4、fastjson序列化乱序问题
1)初始化为有序json对象
JSONObject jsonOrdered= new JSONObject(true);

2)将String对象转换过程中,不要调整顺序
JSONObject jsonOrdered = JSONObject.parseObject(jsonString, Feature.OrderedField);
PS:Feature.OrderedField作用是将String转换Json对象时不要调整顺序(fastJson转换时默认使用HashMap,所以排序规则是根据HASH值排序的)

猜你喜欢

转载自blog.csdn.net/csj50/article/details/80390455
今日推荐