fastjson—Map和List对象间的转换

fastjson—Map和List对象间的转换

fastjson—Map和List对象间的转换

fastjson  是阿里巴巴推出的,一个Java语言编写的高性能JSON 处理器,遵循JSON标准 http://json.org/ ,支持各种JDK 类型。

刚刚看到就觉得爱不释手,还没用过,在接下来的遇到json类的所有操作都可以将这个用上,感觉效率会很高,以至于我看到就有一种强烈的欲望想要分享,或者说是我见识少,哈哈,见识少就要不断的填补空白,把缺的都补上,未来,我也是个大牛,你也是一样。
尤其是在
API测试过程中,API接口返回一堆的json数据,然后就可以对这些数据进行处理了。废话少说,直接介绍:

maven:

fastjson—Map和List对象间的转换

1. List<Map<String, String>> list 转 JSONArray

fastjson—Map和List对象间的转换

JSONArray jsonArray =JSONArray.parseArray(JSONObject.toJSONString(list)); 

/**list List<Map<String,String>>**/

2.JSON 转成List

fastjson—Map和List对象间的转换List<ChannelItem> channelItemList  = JSON.parseArray(itemJson,ChannelItem.class);  /** itemJson: JsonArray  ChannelItem : 对象bean类**/

 

3. Map<String, String> 转 JSON

fastjson—Map和List对象间的转换

JSONObject itemJSONObj = JSONObject.parseObject(JSON.toJSONString(itemMap)); /** itemMap 为 Map<String, String>**/

4.JSON 转 Map<String, String>

fastjson—Map和List对象间的转换

Map<String, Object> itemMap = JSONObject.toJavaObject(itemJSONObj, Map.class);  /** itemJSONObj  JSONObject**/

5. JSONObject 转 JavaBean

fastjson—Map和List对象间的转换

Student student = JSON.parseObject(JSONObjectStr, new TypeReference<Student>() {});  

//因为JSONObject继承了JSON,所以这样也是可以的

6.JSONObject 转 Map<String , String>

fastjson—Map和List对象间的转换

Map<String ,String> param =  JSONObject.parseObject(jsonObject.toJSONString(), new TypeReference<Map<String, String>>(){});

分享到你的朋友圈吧


fastjson—Map和List对象间的转换