淺談JSONObject的使用及示例代碼(JSON解析)
JSONObject只是一種數(shù)據(jù)結(jié)構(gòu),可以理解為JSON格式的數(shù)據(jù)結(jié)構(gòu)(key-value 結(jié)構(gòu)),可以使用put方法給json對(duì)象添加元素。JSONObject可以很方便的轉(zhuǎn)換成字符串,也可以很方便的把其他對(duì)象轉(zhuǎn)換成JSONObject對(duì)象。
簡(jiǎn)介
在程序開(kāi)發(fā)過(guò)程中,在參數(shù)傳遞,函數(shù)返回值等方面,越來(lái)越多的使用JSON。JSON(JavaScript Object Notation)是一種輕量級(jí)的數(shù)據(jù)交換格式,同時(shí)也易于機(jī)器解析和生成、易于理解、閱讀和撰寫(xiě),而且Json采用完全獨(dú)立于語(yǔ)言的文本格式,這使得Json成為理想的數(shù)據(jù)交換語(yǔ)言。
JSON建構(gòu)于兩種結(jié)構(gòu):
“名稱(chēng)/值”對(duì)的集合(A Collection of name/value pairs),在不同的語(yǔ)言中,它被理解為對(duì)象(Object), 記錄(record), 結(jié)構(gòu)(struct), 字典(dictionary), 有趣列表(keyed list), 哈希表(hash table)或者關(guān)聯(lián)數(shù)組(associative array)。
JSONObject依賴(lài):
最后一行需要保留,有兩個(gè)jdk版本的實(shí)現(xiàn):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需要導(dǎo)入的jar包

jar包下載:
鏈接: https://pan.baidu.com/s/1nxwl-R3n6hNVMepT8LWNmw
提取碼: p8w8
JSONObject
創(chuàng)建JSONObject,添加屬性
//創(chuàng)建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);
根據(jù)key返回輸出
System.out.println(json.get("sex"));
判斷輸出對(duì)象的類(lèi)型
boolean isArray = json.isArray();
boolean isEmpty = json.isEmpty();
boolean isNullObject = json.isNullObject();
System.out.println("是否數(shù)組:"+isArray+", 是否空:"+isEmpty+", 是否空為空對(duì)象:"+isNullObject);
把JSONArray添加到JSONObject中
/把JSONArray添加到JSONObject中
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "張三");
jsonArray.add(1, "123");
//開(kāi)始添加
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) {
//創(chuàng)建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);
//根據(jù)key返回
System.out.println(json.get("sex"));
//判斷輸出對(duì)象的類(lèi)型
boolean isArray = json.isArray();
boolean isEmpty = json.isEmpty();
boolean isNullObject = json.isNullObject();
System.out.println("是否數(shù)組:"+isArray+", 是否空:"+isEmpty+", 是否空為空對(duì)象:"+isNullObject);
System.out.println("=====");
//把JSONArray添加到JSONObject中
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "張三");
jsonArray.add(1, "123");
//開(kāi)始添加
json.element("student", jsonArray);
System.out.println(json);
}
}
運(yùn)行結(jié)果:

JSONArray
創(chuàng)建JSONArray,添加屬性值
//創(chuàng)建JSONArray
JSONArray jsonArray = new JSONArray();
//添加
jsonArray.add(0, "張三");
jsonArray.add(1, "123");
jsonArray.element("男");
System.
根據(jù)下標(biāo)返回輸出
System.out.println(jsonArray.get(0));
根據(jù)下標(biāo)設(shè)置新值,修改
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) {
//創(chuàng)建JSONArray
JSONArray jsonArray = new JSONArray();
//添加
jsonArray.add(0, "張三");
jsonArray.add(1, "123");
jsonArray.element("男");
System.out.println(jsonArray);
//根據(jù)下標(biāo)返回輸出
System.out.println(jsonArray.get(0));
//根據(jù)下標(biāo)設(shè)置新值,修改
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);
//循環(huán)輸出
for(int i = 0; i < jsonArray.size(); i++) {
System.out.println(jsonArray.get(i));
}
}
}
運(yùn)行結(jié)果

JavaBean與json字符串互轉(zhuǎn)
student類(lèi):
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 + "]";
}
}
定義對(duì)象,JavaBean對(duì)象轉(zhuǎn)json字符串
//定義對(duì)象
Student stu = new Student("張三", "123456");
//JavaBean對(duì)象轉(zhuǎn)json字符串
JSONObject jsonObject = JSONObject.fromObject(stu);
System.out.println(jsonObject);
json字符串轉(zhuǎn)為javaBean
//json字符串轉(zhuǎn)為javaBean
//定義json字符串
String jsondata = "{\"username\":\"李四\", \"password\":\"123\"}";
//轉(zhuǎn)為json對(duì)象
JSONObject json = JSONObject.fromObject(jsondata);
//轉(zhuǎn)為JavaBean對(duì)象
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) {
//定義對(duì)象
Student stu = new Student("張三", "123456");
//JavaBean對(duì)象轉(zhuǎn)json字符串
JSONObject jsonObject = JSONObject.fromObject(stu);
System.out.println(jsonObject);
//json字符串轉(zhuǎn)為javaBean
//定義json字符串
String jsondata = "{\"username\":\"李四\", \"password\":\"123\"}";
//轉(zhuǎn)為json對(duì)象
JSONObject json = JSONObject.fromObject(jsondata);
//轉(zhuǎn)為JavaBean對(duì)象
Student stu2 = (Student)JSONObject.toBean(json, Student.class);
System.out.println(stu2.toString());
}
}
輸出結(jié)果:

List與json字符串互轉(zhuǎn)
先定義list集合,list轉(zhuǎn)json字符串
//定義list集合
List list = new ArrayList();
list.add(new Student("張三", "123"));
list.add(new Student("李四", "456"));
//list轉(zhuǎn)json字符串
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);
json字符串轉(zhuǎn)list
//json字符串轉(zhuǎn)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轉(zhuǎn)json字符串
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);
//json字符串轉(zhuǎn)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);
}
}
運(yùn)行結(jié)果

Map與json字符串互轉(zhuǎn)
定義map集合,Map轉(zhuǎn)json字符串
//定義map集合
Map map = new HashMap();
map.put("1", new Student("張三", "123"));
map.put("2", new Student("李四", "456"));
//Map轉(zhuǎn)json字符串
JSONObject jsonMap = JSONObject.fromObject(map);
System.out.println(jsonMap);
定義字符串map集合,map集合字符串轉(zhuǎn)為map
//定義字符串map集合
String jsondata = "{\"1\":{\"password\":\"123\",\"username\":\"張三\"},\"2\":{\"password\":\"456\",\"username\":\"李四\"}}";
//map集合字符串轉(zhuǎn)為map
Map map2 = (Map)JSONObject.fromObject(jsondata);
Set set = map2.keySet();
//定義迭代器,迭代輸出
Iterator ite = set.iterator();
while(ite.hasNext()) {
//取出一個(gè)字符串對(duì)象
String key = (String)ite.next();
//轉(zhuǎn)為json格式
JSONObject jsonObject = JSONObject.fromObject(map2.get(key));
//轉(zhuǎn)為對(duì)象
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轉(zhuǎn)json字符串
JSONObject jsonMap = JSONObject.fromObject(map);
System.out.println(jsonMap);
//定義字符串map集合
String jsondata = "{\"1\":{\"password\":\"123\",\"username\":\"張三\"},\"2\":{\"password\":\"456\",\"username\":\"李四\"}}";
//map集合字符串轉(zhuǎn)為map
Map map2 = (Map)JSONObject.fromObject(jsondata);
Set set = map2.keySet();
//定義迭代器,迭代輸出
Iterator ite = set.iterator();
while(ite.hasNext()) {
//取出一個(gè)字符串對(duì)象
String key = (String)ite.next();
//轉(zhuǎn)為json格式
JSONObject jsonObject = JSONObject.fromObject(map2.get(key));
//轉(zhuǎn)為對(duì)象
Student stu = (Student)JSONObject.toBean(jsonObject, Student.class);
System.out.println(key+" "+stu);
}
}
}
運(yùn)行結(jié)果

JSONArray與List互轉(zhuǎn)
定義list集合,List轉(zhuǎn)型JSONArray
//定義list集合
List<Student> list = new ArrayList<Student>();
list.add(new Student("張三", "123"));
list.add(new Student("李四", "456"));
//List轉(zhuǎn)型JSONArray
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray.toString());
JSONArray轉(zhuǎn)型List,JSONArray是用的上面的那個(gè)jsonArray變量
//JSONArray轉(zhuǎn)型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轉(zhuǎn)型JSONArray
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray.toString());
//JSONArray轉(zhuǎn)型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);
}
}
}
運(yùn)行結(jié)果

JSONArray與數(shù)組互轉(zhuǎn)
定義數(shù)組,數(shù)組轉(zhuǎn)JSONArray
//定義數(shù)組
boolean[] boolArray = {true, false, true};
//java數(shù)組轉(zhuǎn)JSONArray
JSONArray jsonArray = JSONArray.fromObject(boolArray);
System.out.println(jsonArray.toString());
JSONArray轉(zhuǎn)java數(shù)組
//JSONArray轉(zhuǎn)java數(shù)組
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) {
//定義數(shù)組
boolean[] boolArray = {true, false, true};
//java數(shù)組轉(zhuǎn)JSONArray
JSONArray jsonArray = JSONArray.fromObject(boolArray);
System.out.println(jsonArray.toString());
//JSONArray轉(zhuǎn)java數(shù)組
Object obj[] = jsonArray.toArray();
for(Object o : obj) {
System.out.print(o+"\t");
}
}
}
運(yùn)行結(jié)果

到此這篇關(guān)于淺談JSONObject的使用及示例代碼(JSON解析)的文章就介紹到這了,更多相關(guān)JSONObject使用方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot實(shí)現(xiàn)用戶名查找用戶功能
本文主要介紹了springboot實(shí)現(xiàn)用戶名查找用戶功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
通過(guò)實(shí)例了解JavaBean開(kāi)發(fā)及使用過(guò)程解析
這篇文章主要介紹了通過(guò)實(shí)例了解JavaBean開(kāi)發(fā)及使用過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
淺談java對(duì)象之間相互轉(zhuǎn)化的多種方式
這篇文章主要介紹了淺談java對(duì)象之間相互轉(zhuǎn)化的多種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
SpringBoot中的Profile多環(huán)境配置方法
這篇文章主要介紹了SpringBoot中的Profile多環(huán)境配置,SpringBoot提供了兩種多環(huán)境配置的方式,分別是使用profile文件進(jìn)行多環(huán)境配置以及使用@Profile注解進(jìn)行多環(huán)境配置,需要的朋友可以參考下2023-01-01

