Skip to content

JSON

2062字约7分钟

javajson

2024-10-24

1.定义

JSON=java Script Object notaiton(js的对象表示)

2.JSON作用?

1.js语言中表示对象

2.在Js进行数据传递(在客户端和服务端进行数据传递)

3.Js中经常被当作配置存在

4.JSON是现代开发的基石,是客户端和服务端数据交互的桥梁。

3.在js中如何使用JSON

4.在java中使用JSON

​ 4款json的java类库 Json-lib、Gson、Jackson、Fastjson(阿里)

5.什么是java bean?

​ bean是特殊的类

  • 具有无参构造方法
  • 所有的属性都是private
  • 属性的访问时通过getter、setter方法进行
    • 一个属性名称,前2为字母,必须同时大写或者同时小写。
  • 普通的类被称作pojo(平的普通的java对象)

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包

image-20210926111949833

点击并拖拽以移动

JSONObject的基本操作

创建JSONObject,添加属性

//创建JSONObject
JSONObject json = new JSONObject();
//添加属性
json.put("username", "test");
json.put("password", "123");
// 删除属性
json.remove("password")
//打印
System.out.println(json);
//增加属性
json.element("sex", "");
”、son.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.remove("password")
        //打印
        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"}
{"username":"test"}
{"age":18,"sex":"","username":"test"}

是否数组:false, 是否空:false, 是否空为空对象:false
=====
{"age":18,"sex":"","student":["test","123"],"username":"test"}

image-20210926210549346

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"}

image-20210926111133791

JavaBean与json字符串互转

student类:

public class Student {
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Student(String username, String password) {
		super();
		this.username = username;
		this.password = password;
	}
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Student [username=" + username + ", password=" + password + "]";
	}
}

定义对象,JavaBean对象转json字符串

//定义对象
Student stu = new Student("test", "123456");
//JavaBean对象转json字符串
JSONObject jsonObject = JSONObject.fromObject(stu);
System.out.println(jsonObject);

json字符串转为javaBean

//json字符串转为javaBean
//定义json字符串
String jsondata = "{\"username\":\"test1\", \"password\":\"123\"}";
//转为json对象
JSONObject json = JSONObject.fromObject(jsondata);
//转为JavaBean对象
Student stu2 = (Student)JSONObject.toBean(json, Student.class);
System.out.println(stu2.toString());

全部代码:

import net.sf.json.JSONObject;
 
public class Json {
	public static void main(String[] args) {
		//定义对象
		Student stu = new Student("test", "123456");
		//JavaBean对象转json字符串
		JSONObject jsonObject = JSONObject.fromObject(stu);
		System.out.println(jsonObject);
		
		//json字符串转为javaBean
		//定义json字符串
		String jsondata = "{\"username\":\"test1\", \"password\":\"123\"}";
		//转为json对象
		JSONObject json = JSONObject.fromObject(jsondata);
		//转为JavaBean对象
		Student stu2 = (Student)JSONObject.toBean(json, Student.class);
		System.out.println(stu2.toString());
	}
}

输出结果:

{"password":"123456","username":"test"}
Student [username=test1, password=123]

image-20210926111437787

List与json字符串互转

先定义list集合,list转json字符串

//定义list集合
List list = new ArrayList();
list.add(new Student("test", "123"));
list.add(new Student("test1", "456"));
//list转json字符串
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);

json字符串转list

//json字符串转list
List list2 = new ArrayList();
String jsondata = "[{\"password\":\"123\",\"username\":\"test\"},{\"password\":\"456\",\"username\":\"test1\"}]";
JSONArray jsonArray1 = JSONArray.fromObject(jsondata);
for(int i = 0; i < jsonArray1.size(); i++) {
	JSONObject jsonObject2 = jsonArray1.getJSONObject(i);
	Student stu2 = (Student)JSONObject.toBean(jsonObject2, Student.class);
	list2.add(stu2);
}
System.out.println(list2);

全部代码

import java.util.ArrayList;
import java.util.List;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
public class Json {
	public static void main(String[] args) {
		//定义list集合
		List list = new ArrayList();
		list.add(new Student("test", "123"));
		list.add(new Student("test1", "456"));
		//list转json字符串
		JSONArray jsonArray = JSONArray.fromObject(list);
		System.out.println(jsonArray);
		
		//json字符串转list
		List list2 = new ArrayList();
		String jsondata = "[{\"password\":\"123\",\"username\":\"test\"},{\"password\":\"456\",\"username\":\"test1\"}]";
		JSONArray jsonArray1 = JSONArray.fromObject(jsondata);
		for(int i = 0; i < jsonArray1.size(); i++) {
			JSONObject jsonObject2 = jsonArray1.getJSONObject(i);
			Student stu2 = (Student)JSONObject.toBean(jsonObject2, Student.class);
			list2.add(stu2);
		}
		System.out.println(list2);
	}
}

运行结果

[{"password":"123","username":"test"},{"password":"456","username":"test1"}]
[Student [username=test, password=123], Student [username=test1, password=456]]

image-20210926111543681

Map与json字符串互转

定义map集合,Map转json字符串

//定义map集合
Map map = new HashMap();
map.put("1", new Student("test", "123"));
map.put("2", new Student("test1", "456"));
//Map转json字符串
JSONObject jsonMap = JSONObject.fromObject(map);
System.out.println(jsonMap);

定义字符串map集合,map集合字符串转为map

//定义字符串map集合
String jsondata = "{\"1\":{\"password\":\"123\",\"username\":\"test\"},\"2\":{\"password\":\"456\",\"username\":\"test1\"}}";
//map集合字符串转为map
Map map2 = (Map)JSONObject.fromObject(jsondata);
Set set = map2.keySet();
//定义迭代器,迭代输出
Iterator ite = set.iterator();
while(ite.hasNext()) {
	//取出一个字符串对象
	String key = (String)ite.next();
	//转为json格式
	JSONObject jsonObject = JSONObject.fromObject(map2.get(key));
	//转为对象
	Student stu = (Student)JSONObject.toBean(jsonObject, Student.class);
	System.out.println(key+"  "+stu);
}

全部代码

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
 
import net.sf.json.JSONObject;
 
public class Json {
	public static void main(String[] args) {
		//定义map集合
		Map map = new HashMap();
		map.put("1", new Student("test", "123"));
		map.put("2", new Student("test1", "456"));
		//Map转json字符串
		JSONObject jsonMap = JSONObject.fromObject(map);
		System.out.println(jsonMap);
		
		//定义字符串map集合
		String jsondata = "{\"1\":{\"password\":\"123\",\"username\":\"test\"},\"2\":{\"password\":\"456\",\"username\":\"test1\"}}";
		//map集合字符串转为map
		Map map2 = (Map)JSONObject.fromObject(jsondata);
		Set set = map2.keySet();
		//定义迭代器,迭代输出
		Iterator ite = set.iterator();
		while(ite.hasNext()) {
			//取出一个字符串对象
			String key = (String)ite.next();
			//转为json格式
			JSONObject jsonObject = JSONObject.fromObject(map2.get(key));
			//转为对象
			Student stu = (Student)JSONObject.toBean(jsonObject, Student.class);
			System.out.println(key+"  "+stu);
		}
	}
}

运行结果

{"1":{"password":"123","username":"test"},"2":{"password":"456","username":"test1"}}
1  Student [username=test, password=123]
2  Student [username=test1, password=456]

image-20210926111634139

JSONArray与List互转

定义list集合,List转型JSONArray

//定义list集合
List<Student> list = new ArrayList<Student>();
list.add(new Student("test", "123"));
list.add(new Student("test1", "456"));
//List转型JSONArray
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray.toString());

JSONArray转型List,JSONArray是用的上面的那个jsonArray变量

//JSONArray转型List
List<Student> list2 = JSONArray.toList(jsonArray, new Student(), new JsonConfig());
Iterator<Student> ite = list2.iterator();
while(ite.hasNext()) {
	Student stu = ite.next();
	System.out.println(stu);
}

全部代码

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
 
public class Json {
	public static void main(String[] args) {
		//定义list集合
		List<Student> list = new ArrayList<Student>();
		list.add(new Student("test", "123"));
		list.add(new Student("test1", "456"));
		//List转型JSONArray
		JSONArray jsonArray = JSONArray.fromObject(list);
		System.out.println(jsonArray.toString());
		
		//JSONArray转型List
		List<Student> list2 = JSONArray.toList(jsonArray, new Student(), new JsonConfig());
		Iterator<Student> ite = list2.iterator();
		while(ite.hasNext()) {
			Student stu = ite.next();
			System.out.println(stu);
		}
	}
}

运行结果

[{"password":"123","username":"test"},{"password":"456","username":"test1"}]
Student [username=test, password=123]
Student [username=test1, password=456]

image-20210926111720943

JSONArray与数组互转

定义数组,数组转JSONArray

//定义数组
boolean[] boolArray = {true, false, true};
//java数组转JSONArray
JSONArray jsonArray = JSONArray.fromObject(boolArray);
System.out.println(jsonArray.toString());

JSONArray转java数组

//JSONArray转java数组
Object obj[] = jsonArray.toArray();
for(Object o : obj) {
	System.out.print(o+"\t");
}

全部代码

import net.sf.json.JSONArray;
 
public class Json {
	public static void main(String[] args) {
		//定义数组
		boolean[] boolArray = {true, false, true};
		//java数组转JSONArray
		JSONArray jsonArray = JSONArray.fromObject(boolArray);
		System.out.println(jsonArray.toString());
		
		//JSONArray转java数组
		Object obj[] = jsonArray.toArray();
		for(Object o : obj) {
			System.out.print(o+"\t");
		}
	}
}

运行结果

[true,false,true]
true	false	true

image-20210926111751788