fastjson 是阿里巴巴推出的,一个Java语言编写的高性能JSON 处理器,遵循JSON标准 http://json.org/ ,支持各种JDK 类型。
maven:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.44</version>
</dependency>
1. List<Map<String, String>> list 转 JSONArray
JSONArray jsonArray = JSONArray.parseArray(JSONObject.toJSONString(list)); /**list为 List<Map<String, String>>**/
2.JSON 转成List
List<ChannelItem> channelItemList = JSON.parseArray(itemJson,ChannelItem.class); /** itemJson: JsonArray ChannelItem : 对象bean类**/
3. Map<String, String> 转 JSON
JSONObject itemJSONObj = JSONObject.parseObject(JSON.toJSONString(itemMap)); /** itemMap 为 Map<String, String>**/
4.JSON 转 Map<String, String>
Map<String, Object> itemMap = JSONObject.toJavaObject(itemJSONObj, Map.class); /** itemJSONObj JSONObject**/
5. JSONObject 转 JavaBean
Student student = JSON.parseObject(JSONObjectStr, new TypeReference<Student>() {}); //因为JSONObject继承了JSON,所以这样也是可以的
6.JSONObject 转 Map<String , String>
Map<String ,String> param = JSONObject.parseObject(jsonObject.toJSONString(), new TypeReference<Map<String, String>>(){});