欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

fastJson的使用

程序员文章站 2022-03-03 14:48:24
...
String、Object、List、Map与json的互转
System.out.println("---------string--------");
//String->json
String str="{\"name\":\"李四\",\"age\":18}";
JSONObject strJson = JSON.parseObject(str);
System.out.println("String->json:"+strJson);
//json->string
String s = strJson.toJSONString();
System.out.println("json->string:"+s);
输出:
---------string--------
String->json:{"name":"李四","age":18}
json->string:{"name":"李四","age":18}


System.out.println("\n---------object--------");
//Object->json
Person person = new Person("张三", 17);
String perStr=JSON.toJSONString(person);
JSONObject objectJson=JSON.parseObject(perStr);
System.out.println("Object->json:"+objectJson);
//json->Object
Person person1 = objectJson.toJavaObject(Person.class);
System.out.println("json->Object:"+person1);
输出:
---------object--------
Object->json:{"name":"张三","age":17}
json->Object:Person{name='张三', age=17}


System.out.println("\n---------list--------");
//list->jsonArray
ArrayList<Object> list = new ArrayList<>();
list.add(new Person("张三", 17));
list.add(new Person("李四", 18));
list.add(new Person("王五", 19));
//1、
String listStr = JSON.toJSONString(list);
JSONArray listJson = JSONArray.parseArray(listStr);
System.out.println("1、list->jsonArray:"+listJson);
//2、
JSONArray listJson2 =new JSONArray(list);
System.out.println("2、list->jsonArray:"+listJson2);
//jsonArray->list
List<Person> arrayLists = listJson.toJavaList(Person.class);
System.out.println("jsonArray->list:"+arrayLists);
输出:
---------list--------
1、list->jsonArray:[{"name":"张三","age":17},{"name":"李四","age":18},{"name":"王五","age":19}]
2、list->jsonArray:[{"age":17,"name":"张三"},{"age":18,"name":"李四"},{"age":19,"name":"王五"}]
jsonArray->list:[Person{name='张三', age=17}, Person{name='李四', age=18}, Person{name='王五', age=19}]


System.out.println("\n---------map--------");
//map->json
HashMap<String, Object> map = new HashMap<>();
map.put("key1",1);
map.put("key2",2);
JSONObject mapJson=new JSONObject(map);
System.out.println("map->json:"+mapJson);
//json->map
Map hashMap = JSON.parseObject(mapJson.toJSONString(), HashMap.class);
System.out.println("json->map:"+hashMap);
输出:
---------map--------
map->json:{"key1":1,"key2":2}
json->map:{key1=1, key2=2}


[size=xx-large][size=small]遍历json数据[/size][/size]
System.out.println("----json----");
//遍历json  使用上面map->json的json对象
Set<String> set = mapJson.keySet();
for (String key:set) {
      System.out.println("key:"+key);
      System.out.println("value:"+mapJson.get(key));
}
输出:
----json----
key:key1
value:1
key:key2
value:2


System.out.println("\n----jsonArray----");
//遍历jsonArray 使用上面list->jsonArray的json数组对象  取出jsonArray中的json再遍历json
for (int i = 0; i <listJson.size() ; i++) {
       JSONObject jo = listJson.getJSONObject(i);
       Set<String> keySet = jo.keySet();
       for (String key:keySet) {
             System.out.println("key:"+key);
             System.out.println("value:"+jo.get(key));
       }
}
输出:
----jsonArray----
key:name
value:张三
key:age
value:17
key:name
value:李四
key:age
value:18
key:name
value:王五
key:age
value:19

 

相关标签: json