fastjson的jsonObject-jsonArray处理
需要引包:
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
pom.xml需要引入:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.44</version>
</dependency>
<artifactId>fastjson</artifactId>
<version>1.2.44</version>
</dependency>
数据格式
//JSONObject的数据是用 { } 来表示的
{ "id":"123", "name":"张三", "age":"11"}
//JSONArray是由JSONObject组成的数组 用【】和{}来表示
[{ "id":"123", "name":"张三", "age":"11"},{ "id":"321", "name":"李四", "age":"21"}]
获取jsonArray中每一个JsonObject里的键对应的内容
String jsonstr = "[{\"id\":\"123\",\"name\":\"张三\",\"age\":\"11\"},{\"id\":\"321\", \"name\":\"李四\",\"age\":\"21\"}]"; JSONArray jsonArray = JSONArray.parseArray(jsonstr); for (int i=0;i<jsonArray.size();i++) {//会循环两次 JSONObject jsonObject = jsonArray.getJSONObject(i); String id = jsonObject.getString("id");//123,321 String name = jsonObject.getString("name");//张三,李四 String age = jsonObject.getString("age");//11,21 }
相关推荐
88483063 2020-06-28
80337960 2020-06-10
88483063 2020-05-25
88103756 2020-05-02
88483063 2020-04-23
ITprivate 2020-03-26
80337960 2020-03-26
80337960 2020-02-22
88483063 2020-01-29
83163452 2020-01-28
baijinswpu 2020-01-25
88483063 2020-01-11
88103756 2020-01-01
88103756 2019-12-24
fengchao000 2019-12-24
80337960 2019-12-23
xufankang 2019-12-19
88483063 2019-12-16
80337960 2019-12-13