目录
- 1.JSONObject介绍
- 2.JSONArray介绍
- 3.从字符串String获得JSONObject对象和JSONArray对象
- 4.从JSONArray中获得JSONObject对象
- 5.获取JSON内的数据
- 5.JSONObject和JSONArray区别
1.JSONObject介绍
JSONObject是阿里巴巴提供的一个json快速转换工具类,实现了Map接口,通过Key Value形式存储数据,初始化大小为16,有参构造方法需要传入一个Map。
JSONObject只是一种数据结构,可以使用put方法给json对象添加元素。JSONObject可以很方便的转换成字符串,也可以很方便的把其他对象转换成JSONObject对象。
- JSONObject继承自JSON类:也即JSONObject可通过toJSONString(Object)将对象序列化为JSON格式,也可通过parseObject(String,
Class),将JSON反序列化为指定的对象。 - JSONObject实现了Map<String, Object>,也即是一个Map类型的数据结构,Map接口提供了很多操作map的方法。
JSONObject的数据是用 { } 来表示的,
例如: { “id” : “123”, “name” : “yyy”, “phone” : “123456789”, “year” : 25 }
2.JSONArray介绍
JSONArray是一个有序的值序列,它的外部文本形式是一个用方括号括起来的字符串,用逗号分隔值,内部表单是具有索引的对象“get”和“opt”用于通过索引访问值的“element”方法,以及用于添加或替换值的方法。
JSONArray,使用中括号[ ],只不过数组里面的项也是json键值对格式的
JSONObject中添加的是键值对,JSONArray中添加的是JSONObject。
JSONArray,顾名思义是由JSONObject构成的数组,用 [ { } , { } , … , { } ] 来表示
例如: [ { “id” : “123”, “name” : “yyy”, “phone” : “123456789”, “year” : 25 } , { “id” : “456”, “name” : “xxx”, “phone” : “987654321”, “year” : 29 } ] ;
表示了包含2个JSONObject的JSONArray。
3.从字符串String获得JSONObject对象和JSONArray对象
JSONObject jsonObject = new JSONObject ( String str);
JSONArray jsonArray = new JSONArray (String str);
JSONObject jsonobject = JSONObject.fromObject(str);//将字符串转化成json对象
JSONArray jsonArray = jsonobj.getJSONArray("date");//获取数组
for(int i=0;i<jsonArray.size();i++){
jsonArray.getJSONObject(i).getString("list")//获取数组里的内容
}
JSONArray jsonArray = JSONArray.fromObject(str);//直接解析成数组
例子:
{
"name": [
"xxx",
"yyy"
]
}
String test = "{\"name\":[\"xxx\",\"yyy\"]}";
JSONObject jsonObject = JSON.parseObject(test); //string转为object类型
System.out.println("jsonObject:" + jsonObject);
JSONArray array = jsonObject.getJSONArray("name"); //输出 ["xxx","yyy"]
System.out.println("array:" + array);
String str = JSONObject.toJSONString(array);
System.out.println("str:" + str);
输出结果:
jsonObject:{"name":["xxx","yyy"]}
array:["xxx","yyy"]
str:["xxx","yyy"]
4.从JSONArray中获得JSONObject对象
JSONObject jsonObject = (JSONObject)jsonArray.get(i);
JSONObject jsonObject = jsonArray.getJSONObject(i) ;
例子:
[
{
"id": "123",
"name": "xxx",
"year": "24",
"content": null
},
{
"id": "345",
"name": "yyy",
"year": "25",
"content": null
}
]
String json = "[{\"id\" :\"123\", \"name\" :\"xxx\", \"year\" :\"24\", \"content\" :null },{\"id\" :\"345\", \"name\" :\"yyy\", \"year\" :\"25\", \"content\" :null }]";
JSONArray jsonArray = JSONArray.parseArray(json);
JSONObject jsonObject = jsonArray.getJSONObject(1); // 这里的jsonObject得到的数据就是第二个JSONObject
System.out.println("jsonObject:" + jsonObject);
输出结果:
jsonObject:{"id":"345","name":"yyy","year":"25"}
注意:用jsonArray获取jsonObject的数据时,取得是下标,下标为0开始
5.获取JSON内的数据
int id = jsonObject.getInt ( "id" ) ; // 这里的jid得到的数据就是123.
String phone = jsonObject.getString( "phone") ;
Strirng year = jsonObject.getString("year");
5.JSONObject和JSONArray区别
简单来说,它们的区别就是:JSONObject是一个对象,JSONArray是一个数组。JSONObject是一个 {}包裹起来的一个对象(Object),而JSONArray则是[ ]包裹起来的一个数组(Array)。
- JSONObject的数据表示形式
{
"id": "123",
"name": "yyy",
"title": "666",
"content": null
}
- JSONArray的数据表示形式(包含2个或2个以上的JSONObject)
[
{
"id": "123",
"name": "yyy",
"title": "666",
"content": null
},
{
"id": "345",
"name": "xxx",
"title": "777",
"content": null
}
]