
1 <dependency>2 <groupId>com.alibaba</groupId>3 <artifactId>fastjson</artifactId>4 <version>1.2.58</version>5 </dependency>
View Code

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 stub16 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 //串转json41 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.get49 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 }
View Code