JSONObject常用方法
-
1.put(String key, Object value)方法,在JSONObject对象中设置键值对在,在进行设值得时候,key是唯一的,如果用相同的key不断设值得时候,保留后面的值。
-
2.Object get(String key) :根据key值获取JSONObject对象中对应的value值,获取到的值是Object类型,需要手动转化为需要的数据类型
-
3.int size():获取JSONObject对象中键值对的数量
-
4.boolean isEmpty():判断该JSONObject对象是否为空
-
5.containsKey(Object key):判断是否有需要的key值
-
6.boolean containsValue(Object value):判断是否有需要的value值
-
7.JSONObject getJSONObject(String key):如果JSONObjct对象中的value是一个JSONObject对象,即根据key获取对应的JSONObject对象;
-
8.JSONArray getJSONArray(String key) :如果JSONObject对象中的value是一个JSONObject数组,既根据key获取对应的JSONObject数组;
-
9.Object remove(Object key):根据key清除某一个键值对。
由于JSONObject是一个map,它还具有map特有的两个方法:
-
10.Set keySet() :获取JSONObject中的key,并将其放入Set集合中
-
11.Set<Map.Entry<String, Object>> entrySet():在循环遍历时使用,取得是键和值的映射关系,Entry就是Map接口中的内部接口
1、JSONObject和JSONArray的使用
- 使用场景
- 想通过键值对的形式获取数据,使用JSONObject
- 如果后台查询的是某个bean的list集合向前端页面传递,使用JSONArray
2、JSONObject和JSONArray的区别
2.1 JSONObject的数据表示形式
{
"id":"1",
"name":"张三",
“title:"测试",
"content":null
}
//{ "id" : "1", "name" : "张三", "title" : "测试", "content" : null }
2.2 JSONArray的数据表示形式(包含2个或2个以上的JSONObject)
[
{
"id":"1",
"name":"张三",
“title:"测试1",
"content":null
},
{
"id":"2",
"name":"李四",
“title:"测试2",
"content":null
}
]
//[{ "id" : "1", "name" : "张三", "title" : "测试1", "content" : null } ,
//{ "id" : "2", "name" : "李四", "title" : "测试2", "content" : null }];
备注
: JSONObject最外面用的是{}
,JSONArray最外面用的是[]
3、从字符串Sstring获得JSONObject对象和JSONArray对象
{
"name":[
"toms",
"lisa"
]
}
@Test
public void getJSONObjectAndJSONArray(){
String test = "{\"name\":[\"toms\",\"lisa\"]}";
JSONObject jsonObject = JSON.parseObject(test);
System.out.println("======================");
System.out.println("jsonObject: "+jsonObject);
JSONArray array = jsonObject.getJSONArray("name");
System.out.println("======================");
System.out.println("array: "+array);
String str = JSONObject.toJSONString(array);
System.out.println("======================");
System.out.println("str: "+str);
}
4、从JSONArray中获得JSONObject对象
备注
:可以把JSONArray当成一般的数组来对待,只是获取的数据内数据的方法不一样
JSONArray array = jsonObject.getJSONArray(i);
-
例1: 使用getJSONObject(i) 要注意数组越界异常
@Test public void getJsonObject1(){ String jsonString = "[{ \"id\" : \"1\", \"name\" : \"张三\", \"title\" : \"测试1\", \"content\" : null } , { \"id\" : \"2\", \"name\" : \"李四\", \"title\" : \"测试2\", \"content\" : null }]"; JSONArray jsonArray = JSONArray.parseArray(jsonString); JSONObject jsonObject = jsonArray.getJSONObject(1); System.out.println("========================================="); System.out.println("jsonObject: "+jsonObject); }
-
例2:
{ "id": "1", "name": "张三", "content": [ { "age": "20", "sex": "男" } ] }
@Test public void getJsonObject2(){ String jsonString ="{\"id\":\"100\",\"name\":\"张三\",\"content\":[{\"age\":\"20\",\"sex\":\"男\"}]}"; JSONObject jsonObject = JSON.parseObject(jsonString); JSONArray jsonArray = jsonObject.getJSONArray("content"); System.out.println("jsonArray: "+jsonArray); System.out.println("=============================="); for (int i = 0; i < jsonArray.size(); i++) { //第一种 JSONObject jsonObject1 = (JSONObject)jsonArray.get(i); String age = String.valueOf(jsonObject1.get("age")); System.out.println("===================================="); System.out.println("jsonObject1: "+age); //第二种 JSONObject jsonObject2 = jsonArray.getJSONObject(i); String age1 = jsonObject2.getString("age"); System.out.println("jsonObject2: "+age1); } }
5、获取JSON内的数据
{
"id": "1",
"name": "张三",
"title": "测试",
"content": null
}
@Test
public void getJson(){
String jsonString ="{\"id\":\"1\",\"name\":\"张三\",\"content\":null}";
JSONObject jsonObject = JSON.parseObject(jsonString);
String id = jsonObject.getString("id");
String name = jsonObject.getObject("name", String.class);
System.out.println("id: "+id);
System.out.println("name: "+name);
}
6、给JsonObject中 put值
@Test
public void getJsonObjectToPut(){
String jsonString ="{\"id\":\"1\",\"name\":\"张三\",\"content\":null}";
JSONObject jsonObject = JSON.parseObject(jsonString);
//给jsonObject中添加元素 age
jsonObject.put("age",20);
System.out.println(jsonObject);
//给jsonObject中添加 已存在的key,会替换原有的value值
jsonObject.put("age",30);
System.out.println(jsonObject);
}
7、JSONObject.put和JSONObject.fluentPut的区别
//fluentPut方法源码
public JSONObject fluentPut(String key, Object value) {
this.map.put(key, value);
return this;
}
// put方法源码
public Object put(String key, Object value) {
return this.map.put(key, value);
}