JSON建构于两种结构:
JSON是“名称/值”对的集合(A Collection of name/value pairs),在不同的语言中,它被理解为对象(Object), 记录(record), 结构(struct), 字典(dictionary), 有序列表(keyed list), 哈希表(hash table)或者关联数组(associative array)。
JSONObject依赖:
最后一行需要保留,有两个jdk版本的实现:json-lib-2.1-jdk13.jar和json-lib-2.1-jdk15.jar
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
使用net.sf.json需要导入的jar包
JSONObject的基本操作
创建JSONObject,添加属性
//创建JSONObject
JSONObject json = new JSONObject();
//添加属性
json.put("username", "test");
json.put("password", "123");
//打印
System.out.println(json);
//增加属性
json.element("sex", "男");
json.put("age", 18);
System.out.println(json);
根据key返回输出
System.out.println(json.get("sex"));
判断输出对象的类型
boolean isArray = json.isArray();
boolean isEmpty = json.isEmpty();
boolean isNullObject = json.isNullObject();
System.out.println("是否数组:"+isArray+", 是否空:"+isEmpty+", 是否空为空对象:"+isNullObject);
把JSONArray添加到JSONObject中
//把JSONArray添加到JSONObject中
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "test");
jsonArray.add(1, "123");
//开始添加
json.element("student", jsonArray);
System.out.println(json);
全部代码:
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Json {
public static void main(String[] args) {
//创建JSONObject
JSONObject json = new JSONObject();
//添加属性
json.put("username", "test");
json.put("password", "123");
//打印
System.out.println(json);
//增加属性
json.element("sex", "男");
json.put("age", 18);
System.out.println(json);
//根据key返回
System.out.println(json.get("sex"));
//判断输出对象的类型
boolean isArray = json.isArray();
boolean isEmpty = json.isEmpty();
boolean isNullObject = json.isNullObject();
System.out.println("是否数组:"+isArray+", 是否空:"+isEmpty+", 是否空为空对象:"+isNullObject);
System.out.println("=====");
//把JSONArray添加到JSONObject中
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "test");
jsonArray.add(1, "123");
//开始添加
json.element("student", jsonArray);
System.out.println(json);
}
}
运行结果:
{"password":"123","username":"test"} {"age":18,"password":"123","sex":"男","username":"test"} 男 是否数组:false, 是否空:false, 是否空为空对象:false ===== {"age":18,"password":"123","sex":"男","student":["test","123"],"username":"test"}
JSONArray的基本操作
创建JSONArray,添加属性值
//创建JSONArray
JSONArray jsonArray = new JSONArray();
//添加
jsonArray.add(0, "test");
jsonArray.add(1, "123");
jsonArray.element("男");
根据下标返回输出
System.out.println(jsonArray.get(0));
根据下标设置新值,修改
jsonArray.set(0, "test1");
System.out.println(jsonArray);
把JSONObject放入到JSONArray中
//把JSONObject放入到JSONArray中
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", "test");
jsonObject.put("password", "123");
jsonArray.add(jsonObject);
全部代码:
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Json {
public static void main(String[] args) {
//创建JSONArray
JSONArray jsonArray = new JSONArray();
//添加
jsonArray.add(0, "test");
jsonArray.add(1, "123");
jsonArray.element("男");
System.out.println(jsonArray);
//根据下标返回输出
System.out.println(jsonArray.get(0));
//根据下标设置新值,修改
jsonArray.set(0, "test1");
System.out.println(jsonArray);
//把JSONObject放入到JSONArray中
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", "test");
jsonObject.put("password", "123");
jsonArray.add(jsonObject);
System.out.println(jsonArray);
//循环输出
for(int i = 0; i < jsonArray.size(); i++) {
System.out.println(jsonArray.get(i));
}
}
}
运行结果
["test","123","男"] test ["test1","123","男"] ["test1","123","男",{"password":"123","username":"test"}] test1 123 男 {"password":"123","username":"test"}