pom
<properties>
<fastjson.version>1.2.31</fastjson.version>
</properties>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
1.Map转JSON
Map<String, Object> map = new HashMap<String, Object>();
map.put("username", "yaomy");
map.put("password", "123");
JSONObject json = new JSONObject(map);
2.JSON转Map
JSONObject json = new JSONObject();
json.put("username", "yaomy");
json.put("password", "123");
Map<String, Object> map = (Map<String, Object>)json;
3.JSON转String
JSONObject json = new JSONObject();
json.put("username", "yaomy");
json.put("password", "123");
json.toJSONString();
4.String转JSON
String str = "{\"username\":\"yaomy\",\"password\":\"123\"}";
JSONObject json = JSONObject.parseObject(str);
5.List转JSONArray
List<T> list = new ArrayList<T>();
JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));
6.JSONArray转List
JSONArray array = new JSONArray();
List<EventColAttr> list = JSONObject.parseArray(array.toJSONString(), EventColAttr.class);
7.String转JSONArray
String st = "[{name:zhangsan,age:25,sex:male},{name:lisi,age:28,sex:male},{name:wangwu,age:15,sex:female}]";
JSONArray tableData = JSONArray.parseArray(st);
8.JsonArray转String
String jsonString = jsonArray.toString();