【JSON解析】淺談JSONObject的使用

一枕江風發表於2020-03-29

簡介

在程式開發過程中,在引數傳遞,函式返回值等方面,越來越多的使用JSON。JSON(JavaScript Object Notation)是一種輕量級的資料交換格式,同時也易於機器解析和生成、易於理解、閱讀和撰寫,而且Json採用完全獨立於語言的文字格式,這使得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包

jar包下載:https://pan.baidu.com/s/1iZiXw55TPwIxYFQQCaR9Gw

JSONObject

建立JSONObject,新增屬性

//建立JSONObject
JSONObject json = new JSONObject();
//新增屬性
json.put("username", "張三");
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, "張三");
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", "張三");
		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, "張三");
		jsonArray.add(1, "123");
		//開始新增
		json.element("student", jsonArray);
		System.out.println(json);
	}
}

執行結果:

JSONArray

建立JSONArray,新增屬性值

//建立JSONArray
JSONArray jsonArray = new JSONArray();
//新增
jsonArray.add(0, "張三");
jsonArray.add(1, "123");
jsonArray.element("男");
System.

根據下標返回輸出

System.out.println(jsonArray.get(0));

根據下標設定新值,修改

jsonArray.set(0, "李四");
System.out.println(jsonArray);

把JSONObject放入到JSONArray中

//把JSONObject放入到JSONArray中
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", "張三");
jsonObject.put("password", "123");
jsonArray.add(jsonObject);
System.

全部程式碼:

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, "張三");
		jsonArray.add(1, "123");
		jsonArray.element("男");
		System.out.println(jsonArray);
		
		//根據下標返回輸出
		System.out.println(jsonArray.get(0));
		
		//根據下標設定新值,修改
		jsonArray.set(0, "李四");
		System.out.println(jsonArray);
		
		//把JSONObject放入到JSONArray中
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("username", "張三");
		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));
		}
	}
}

執行結果

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("張三", "123456");
//JavaBean物件轉json字串
JSONObject jsonObject = JSONObject.fromObject(stu);
System.out.println(jsonObject);

json字串轉為javaBean

//json字串轉為javaBean
//定義json字串
String jsondata = "{\"username\":\"李四\", \"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("張三", "123456");
		//JavaBean物件轉json字串
		JSONObject jsonObject = JSONObject.fromObject(stu);
		System.out.println(jsonObject);
		
		//json字串轉為javaBean
		//定義json字串
		String jsondata = "{\"username\":\"李四\", \"password\":\"123\"}";
		//轉為json物件
		JSONObject json = JSONObject.fromObject(jsondata);
		//轉為JavaBean物件
		Student stu2 = (Student)JSONObject.toBean(json, Student.class);
		System.out.println(stu2.toString());
	}
}

輸出結果:

List與json字串互轉

先定義list集合,list轉json字串

//定義list集合
List list = new ArrayList();
list.add(new Student("張三", "123"));
list.add(new Student("李四", "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\":\"張三\"},{\"password\":\"456\",\"username\":\"李四\"}]";
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("張三", "123"));
		list.add(new Student("李四", "456"));
		//list轉json字串
		JSONArray jsonArray = JSONArray.fromObject(list);
		System.out.println(jsonArray);
		
		//json字串轉list
		List list2 = new ArrayList();
		String jsondata = "[{\"password\":\"123\",\"username\":\"張三\"},{\"password\":\"456\",\"username\":\"李四\"}]";
		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);
	}
}

執行結果

Map與json字串互轉

定義map集合,Map轉json字串

//定義map集合
Map map = new HashMap();
map.put("1", new Student("張三", "123"));
map.put("2", new Student("李四", "456"));
//Map轉json字串
JSONObject jsonMap = JSONObject.fromObject(map);
System.out.println(jsonMap);

定義字串map集合,map集合字串轉為map

//定義字串map集合
String jsondata = "{\"1\":{\"password\":\"123\",\"username\":\"張三\"},\"2\":{\"password\":\"456\",\"username\":\"李四\"}}";
//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("張三", "123"));
		map.put("2", new Student("李四", "456"));
		//Map轉json字串
		JSONObject jsonMap = JSONObject.fromObject(map);
		System.out.println(jsonMap);
		
		//定義字串map集合
		String jsondata = "{\"1\":{\"password\":\"123\",\"username\":\"張三\"},\"2\":{\"password\":\"456\",\"username\":\"李四\"}}";
		//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);
		}
	}
}

執行結果

JSONArray與List互轉

定義list集合,List轉型JSONArray

//定義list集合
List<Student> list = new ArrayList<Student>();
list.add(new Student("張三", "123"));
list.add(new Student("李四", "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("張三", "123"));
		list.add(new Student("李四", "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);
		}
	}
}

執行結果

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

執行結果

 

相關文章