fastjson json数据处理
Posted 18513757531
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了fastjson json数据处理相关的知识,希望对你有一定的参考价值。
1 <dependency> 2 <groupId>com.alibaba</groupId> 3 <artifactId>fastjson</artifactId> 4 <version>1.2.58</version> 5 </dependency>
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 = " " + 17 "{ " + 18 " "count":20, " + 19 " "start":0, " + 20 " "total":641, " + 21 " "books":[ " + 22 " { " + 23 " "id":"4866934", " + 24 " "title":"Python基础教程" " + 25 " }, " + 26 " { " + 27 " "id":"3117898", " + 28 " "title":"Python源码剖析" " + 29 " }, " + 30 " { " + 31 " "id":"3948354", " + 32 " "title":"Python学习手册" " + 33 " }, " + 34 " { " + 35 " "id":"3884108", " + 36 " "title":"可爱的Python" " + 37 " } " + 38 " ] " + 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 }
以上是关于fastjson json数据处理的主要内容,如果未能解决你的问题,请参考以下文章