fastjson json数据处理
jar包
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <dependency> 2 <groupId>com.alibaba</groupId> 3 <artifactId>fastjson</artifactId> 4 <version>1.2.58</version> 5 </dependency>
JsonObject
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.baidu.com; 2 3 4 5 import java.util.Map; 6 7 import com.alibaba.fastjson.JSON; 8 import com.alibaba.fastjson.JSONArray; 9 import com.alibaba.fastjson.JSONObject; 10 11 12 public class JsonTest { 13 14 public static void main(String[] args) { 15 // TODO Auto-generated method stub 16 String jsonString = "\r\n" + 17 "{\r\n" + 18 " \"count\":20,\r\n" + 19 " \"start\":0,\r\n" + 20 " \"total\":641,\r\n" + 21 " \"books\":[\r\n" + 22 " {\r\n" + 23 " \"id\":\"4866934\",\r\n" + 24 " \"title\":\"Python基础教程\"\r\n" + 25 " },\r\n" + 26 " {\r\n" + 27 " \"id\":\"3117898\",\r\n" + 28 " \"title\":\"Python源码剖析\"\r\n" + 29 " },\r\n" + 30 " {\r\n" + 31 " \"id\":\"3948354\",\r\n" + 32 " \"title\":\"Python学习手册\"\r\n" + 33 " },\r\n" + 34 " {\r\n" + 35 " \"id\":\"3884108\",\r\n" + 36 " \"title\":\"可爱的Python\"\r\n" + 37 " }\r\n" + 38 " ]\r\n" + 39 "}"; 40 //串转json 41 JSONObject result01 = (JSONObject) JSON.parse(jsonString); 42 System.out.println(result01); 43 44 //获取json数组 45 System.out.println(result01.get("total")); 46 //判断json的value的数据类型 47 System.out.println(result01.get("total").toString().startsWith("641")); 48 //result01.get 49 JSONArray result02 = result01.getJSONArray("employees"); 50 51 System.out.println("33"); 52 53 // json数据中下一维度所有key字段获取 54 JSONObject jsonObj = JSON.parseObject(jsonString); 55 for (Map.Entry<String, Object> entry : jsonObj.entrySet()) { 56 System.out.println(entry.getKey() + ":" + entry.getValue()); 57 58 } 59 } 60 61 }
JsonXpath
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //jsonpath 2 //判断串是否是json格式 json对象 json数组 3 //String string2 = "dd"; 4 String string2 = "{\"total\":641,\"books\":[{\"id\":\"4866934\",\"title\":\"Python基础教程\"},{\"id\":\"3117898\",\"title\":\"Python源码剖析\"},{\"id\":\"3948354\",\"title\":\"Python学习手册\"},{\"id\":\"3884108\",\"title\":\"可爱的Python\"}],\"count\":20,\"start\":0}\r\n"; 5 //String string2 = "[{\"id\":\"4866934\",\"title\":\"Python基础教程\"},{\"id\":\"3117898\",\"title\":\"Python源码剖析\"},{\"id\":\"3948354\",\"title\":\"Python学习手册\"},{\"id\":\"3884108\",\"title\":\"可爱的Python\"}]"; 6 System.out.println(JSON.isValid(string2)); 7 System.out.println(JSON.isValidObject(string2)); 8 System.out.println(JSON.isValidArray(string2)); 9 10 int result03 = (Integer) JSONPath.read(string2, "$.total"); 11 int result031 = (Integer) JSONPath.read(string2, "total"); 12 List<Integer> result0311 = (List<Integer>) JSONPath.read(string2, "books.id"); 13 List<Integer> result03111 = (List<Integer>) JSONPath.read(string2, "..id");//相对路径 14 System.out.println(result03); 15 System.out.println(result031); 16 System.out.println(result0311.size()+" "+result0311.get(0)); 17 System.out.println(result03111.size()+" "+result03111.get(0));
Json数据转换
1 //json数据转换 2 //String string21 = "{\"total\":641,\"books\":[{\"id\":\"4866934\",\"title\":\"Python基础教程\"},{\"id\":\"3117898\",\"title\":\"Python源码剖析\"},{\"id\":\"3948354\",\"title\":\"Python学习手册\"},{\"id\":\"3884108\",\"title\":\"可爱的Python\"}],\"count\":20,\"start\":0}\r\n"; 3 String string21 = "{\"zz\":\"dd\",\"total\":641,\"books\":[{\"zz\":\"dd\",\"id\":\"4866934\",\"title\":\"Python基础教程\"},{\"zz\":\"dd\",\"id\":\"3117898\",\"title\":\"Python源码剖析\"},{\"id\":\"3948354\",\"zz\":\"dd\",\"title\":\"Python学习手册\"},{\"zz\":\"dd\",\"id\":\"3884108\",\"title\":\"可爱的Python\"}],\"count\":20,\"start\":0}"; 4 JSONObject result04 = (JSONObject)JSON.parse(string21); 5 Map result041 = JSON.parseObject(string21, TreeMap.class);//下一层级转换成map 并进行排序 6 String result05 = JSON.toJSONString(JSON.parse(string21)); 7 8 System.out.println(result04); 9 System.out.println(result041); 10 System.out.println(result05); 11 12 //自己构造json 13 JSONObject jsonObject= new JSONObject(); 14 jsonObject.put("ds", "s"); 15 jsonObject.put("dds", "ss"); 16 System.out.println(jsonObject);
Json按照一个规则进行key排序
1 //json数据按照A-Z排序 2 //String string212 = "{\"zz\":\"dd\",\"total\":641,\"books\":[{\"zz\":\"dd\",\"id\":\"4866934\",\"title\":\"Python基础教程\"},{\"zz\":\"dd\",\"id\":\"3117898\",\"title\":\"Python源码剖析\"},{\"id\":\"3948354\",\"zz\":\"dd\",\"title\":\"Python学习手册\"},{\"zz\":\"dd\",\"id\":\"3884108\",\"title\":\"可爱的Python\"}],\"count\":20,\"start\":0}"; 3 String string212 = "{\"zz\":\"dd\",\"Zz\":\"dd\",\"ZA\":\"dd\",\"total\":641,\"books\":[{\"zz\":\"dd\",\"id\":\"4866934\",\"title\":\"Python基础教程\"},{\"zz\":\"dd\",\"id\":\"3117898\",\"title\":\"Python源码剖析\"},{\"id\":\"3948354\",\"zz\":\"dd\",\"title\":\"Python学习手册\"},{\"zz\":\"dd\",\"id\":\"3884108\",\"title\":\"可爱的Python\"}],\"count\":20,\"start\":0}"; 4 System.out.println(string212); 5 String tt =JSONObject.toJSONString((JSONObject)JSON.parse(string212),SerializerFeature.SortField.MapSortField); 6 System.out.println(tt);