需要包:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.43</version>
</dependency>
1、 Json字符串转 map 对象
String jsonString1 = "{\"param5\":\"value5\",\"param3\":\"value3\",\"param4\":\"value4\",\"param1\":\"value1\",\"param2\":\"value2\"}";
System.out.println(jsonString1);
Map<String,String> stringStringMap = (Map<String,String>) JSON.parse(jsonString1);
for (String s : stringStringMap.keySet()) {
System.out.println(s + "==>" +stringStringMap.get(s));
}
输出:
{"param5":"value5","param3":"value3","param4":"value4","param1":"value1","param2":"value2"}
param5==>value5
param3==>value3
param4==>value4
param1==>value1
param2==>value2
2、Json字符串转 List 集合
String jsonString2 = "[{\"param5\":\"value5\",\"param3\":\"value3\",\"param4\":\"value4\",\"param1\":\"value1\",\"param2\":\"value2\"},{\"p1\":\"v1\",\"p2\":\"v2\",\"p3\":\"v3\",\"p4\":\"v4\",\"p5\":\"v5\"}]";
System.out.println(jsonString2);
List<Map<String,String>> mapList = JSON.parseObject(jsonString2, new TypeReference<List<Map<String,String>>>(){});
for (Map<String, String> stringObjectMap : mapList) {
for (String s : stringObjectMap.keySet()) {
System.out.println(s + "==>" + stringObjectMap.get(s));
}
}
输出:
[{"param5":"value5","param3":"value3","param4":"value4","param1":"value1","param2":"value2"},{"p1":"v1","p2":"v2","p3":"v3","p4":"v4","p5":"v5"}]
param5==>value5
param3==>value3
param4==>value4
param1==>value1
param2==>value2
p1==>v1
p2==>v2
p3==>v3
p4==>v4
p5==>v5
3、复杂JSON字符串 转 List集合
String jsonString3 = "{\"count\":2,\"list\":[{\"param5\":\"value5\",\"param3\":\"value3\",\"param4\":\"value4\",\"param1\":\"value1\",\"param2\":\"value2\"},{\"p1\":\"v1\",\"p2\":\"v2\",\"p3\":\"v3\",\"p4\":\"v4\",\"p5\":\"v5\"}]}";
System.out.println(jsonString3);
Map<String,Object> map = JSON.parseObject(jsonString3);
System.out.println(map.get("count"));
String tempjsonString3 = map.get("list").toString();
System.out.println(tempjsonString3);
List<Map<String,String>> mapList2 = JSON.parseObject(tempjsonString3, new TypeReference<List<Map<String,String>>>(){});
for (Map<String, String> stringObjectMap : mapList2) {
for (String s : stringObjectMap.keySet()) {
System.out.println(s + "==>" + stringObjectMap.get(s));
}
}
输出:
{"count":2,"list":[{"param5":"value5","param3":"value3","param4":"value4","param1":"value1","param2":"value2"},{"p1":"v1","p2":"v2","p3":"v3","p4":"v4","p5":"v5"}]}
2
[{"param5":"value5","param3":"value3","param4":"value4","param1":"value1","param2":"value2"},{"p1":"v1","p2":"v2","p3":"v3","p4":"v4","p5":"v5"}]
param5==>value5
param3==>value3
param4==>value4
param1==>value1
param2==>value2
p1==>v1
p2==>v2
p3==>v3
p4==>v4
p5==>v5
4、解析JSON字符串到指定对象class
第一种方式,使用parseArray
String jsonString4 = "[{\"age\":12,\"date\":1465475917155,\"name\":\"s1\"},{\"age\":12,\"date\":1465475917175,\"name\":\"s2\"},{\"age\":12,\"date\":1465475917175,\"name\":\"s3\"},{\"age\":12,\"date\":1465475917175,\"name\":\"s4\"},{\"age\":12,\"date\":1465475917175,\"name\":\"s5\"},{\"age\":12,\"date\":1465475917175,\"name\":\"s6\"}]";
System.out.println(jsonString4);
List<Student> studentList = JSON.parseArray(jsonString4,Student.class);
for (Student student : studentList) {
System.out.println(student);
}
第二种方式,使用parseObject
System.out.println(jsonString4);
List<Student> studentList2 = JSON.parseObject(jsonString4,new TypeReference<List<Student>>(){});
for (Student student : studentList2) {
System.out.println(student);
}
输出:
[{"age":12,"date":1465475917155,"name":"s1"},{"age":12,"date":1465475917175,"name":"s2"},{"age":12,"date":1465475917175,"name":"s3"},{"age":12,"date":1465475917175,"name":"s4"},{"age":12,"date":1465475917175,"name":"s5"},{"age":12,"date":1465475917175,"name":"s6"}]
Student{name='s1', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s2', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s3', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s4', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s5', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s6', age=12, date=Thu Jun 09 20:38:37 CST 2016}
5、 Map对象转化成json字符串
Map<String, Object> maps = new HashMap<String,Object>();
maps.put("grant_code","XF9JKY0R");
maps.put("is_car_config",0);
maps.put("isCN",1);
maps.put("vinCode","LGBG22E22AY081092");
String jsonParam = JSON.toJSONString(maps);
System.out.println(jsonParam);
输出:
{"grant_code":"XF9JKY0R","is_car_config":0,"vinCode":"LGBG22E22AY081092","isCN":1}
6、综合运用
package com.sc.fastjson;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FastJsonDemo {
public static void main(String[] args) {
// ObjectToJson();
JsonToString();
}
public static void ObjectToJson(){
Map<String,Object> map = new HashMap<String,Object>();
map.put("id","123");
map.put("name","张三");
map.put("sex","男");
map.put("age","15");
/**
* 1、先将map 进行json格式化,JSON.toJSONString(map) 返回json字符串
* 2、然后将json字符串转换成对象
*/
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(map));
String age = jsonObject.get("age").toString();
System.out.println(age);//15
List<String> list = new ArrayList<String>();
list.add("{a,b,c}");
list.add("{a1,b1,c1}");
list.add("{a2,b2,c2}");
list.add("{a3,b3,c3}");
JSONArray jsonArray = JSON.parseArray(JSON.toJSONString(list));
System.out.println(jsonArray.get(2));//{a2,b2,c2}
map.put("list",list);
JSONObject jsonObject1 = JSON.parseObject(JSON.toJSONString(map));
JSONArray jsonArray1 = jsonObject1.getJSONArray("list");
System.out.println(jsonArray1.get(3));//{a3,b3,c3}
}
public static void JsonToString(){
String jsonStr1 = "{\"param5\":\"value5\",\"param3\":\"value3\",\"param4\":\"value4\",\"param1\":\"value1\",\"param2\":\"value2\"}";
Object object = JSON.parse(jsonStr1);
System.out.println(object.toString());
Map<String,Object> map = (Map<String, Object>) JSON.parse(jsonStr1);
System.out.println(map.get("param4").toString());
System.out.println("---------第一种写法----------");
//复杂json转换成List
//第一种写法
String jsonStr2 = "{\"count\":2,\"list\":[{\"param5\":\"value5\",\"param3\":\"value3\",\"param4\":\"value4\",\"param1\":\"value1\",\"param2\":\"value2\"},{\"p1\":\"v1\",\"p2\":\"v2\",\"p3\":\"v3\",\"p4\":\"v4\",\"p5\":\"v5\"}]}";
JSONObject jsonObject = (JSONObject) JSON.parse(jsonStr2);
JSONArray array = (JSONArray) jsonObject.get("list");
for (Object o : array){
System.out.println(o.toString());
}
System.out.println("---------第二种写法----------");
//第二种写法
String jsonStr3 = "{\"count\":2,\"list\":[{\"param5\":\"value5\",\"param3\":\"value3\",\"param4\":\"value4\",\"param1\":\"value1\",\"param2\":\"value2\"},{\"p1\":\"v1\",\"p2\":\"v2\",\"p3\":\"v3\",\"p4\":\"v4\",\"p5\":\"v5\"}]}";
Map<String,Object> map2 = (Map<String, Object>) JSON.parse(jsonStr3);
List<Object> list2 = (List<Object>) map2.get("list");
for(Object o:list2){
System.out.println(o.toString());
}
System.out.println("---------第三种写法-------------");
//第三种写法
String jsonStr4 = "{\"count\":2,\"list\":[{\"param5\":\"value5\",\"param3\":\"value3\",\"param4\":\"value4\",\"param1\":\"value1\",\"param2\":\"value2\"},{\"p1\":\"v1\",\"p2\":\"v2\",\"p3\":\"v3\",\"p4\":\"v4\",\"p5\":\"v5\"}]}";
Map<String,Object> map4 = JSON.parseObject(jsonStr4,new TypeReference<Map<String,Object>>(){});
String jsonList = map4.get("list").toString();
List<Object> list4 =JSON.parseObject(jsonList,new TypeReference<List<Object>>(){});
for(Object o : list4){
System.out.println(o.toString());
}
System.out.println("---------第四种写法-------------");
String jsonStr5 = "{\"count\":2,\"list\":[{\"param5\":\"value5\",\"param3\":\"value3\",\"param4\":\"value4\",\"param1\":\"value1\",\"param2\":\"value2\"},{\"p1\":\"v1\",\"p2\":\"v2\",\"p3\":\"v3\",\"p4\":\"v4\",\"p5\":\"v5\"}]}";
JSONObject jsonEntry = JSON.parseObject(jsonStr5);
for(Map.Entry<String,Object> entry:jsonEntry.entrySet()){
if(entry.getKey().equals("list")){
JSONArray jsonArray = JSON.parseArray(entry.getValue().toString());
for(Object o: jsonArray){
System.out.println(o.toString());
}
}
}
}
}
以上输出结果为:
{"param5":"value5","param3":"value3","param4":"value4","param1":"value1","param2":"value2"}
{"p1":"v1","p2":"v2","p3":"v3","p4":"v4","p5":"v5"}