最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java使用JSONObject操作json實例解析

 更新時間:2020年04月11日 10:08:46   作者:一枕江風(fēng)  
這篇文章主要介紹了Java使用JSONObject操作json,結(jié)合實例形式較為詳細的分析了Java使用JSONObject解析json數(shù)據(jù)相關(guān)原理、使用技巧與操作注意事項,需要的朋友可以參考下

本文實例講述了Java使用JSONObject操作json。分享給大家供大家參考,具體如下:

簡介

在程序開發(fā)過程中,在參數(shù)傳遞,函數(shù)返回值等方面,越來越多的使用JSON。JSON(JavaScript Object Notation)是一種輕量級的數(shù)據(jù)交換格式,同時也易于機器解析和生成、易于理解、閱讀和撰寫,而且Json采用完全獨立于語言的文本格式,這使得Json成為理想的數(shù)據(jù)交換語言。 
JSON建構(gòu)于兩種結(jié)構(gòu):

“名稱/值”對的集合(A Collection of name/value pairs),在不同的語言中,它被理解為對象(Object), 記錄(record), 結(jié)構(gòu)(struct), 字典(dictionary), 有趣列表(keyed list), 哈希表(hash table)或者關(guān)聯(lián)數(shù)組(associative array)。

JSONObject依賴:

最后一行需要保留,有兩個jdk版本的實現(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/1iZiXw55TPwIxYFQQCaR9Gw

或者點擊此處本站下載。

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

判斷輸出對象的類型

boolean isArray = json.isArray();
boolean isEmpty = json.isEmpty();
boolean isNullObject = json.isNullObject();
System.out.println("是否數(shù)組:"+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) {
		//創(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"));
		
		//判斷輸出對象的類型
		boolean isArray = json.isArray();
		boolean isEmpty = json.isEmpty();
		boolean isNullObject = json.isNullObject();
		System.out.println("是否數(shù)組:"+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);
	}
}

運行結(jié)果:

JSONArray

創(chuàng)建JSONArray,添加屬性值

//創(chuàng)建JSONArray
JSONArray jsonArray = new JSONArray();
//添加
jsonArray.add(0, "張三");
jsonArray.add(1, "123");
jsonArray.element("男");
System.

根據(jù)下標返回輸出

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

根據(jù)下標設(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ù)下標返回輸出
		System.out.println(jsonArray.get(0));
		
		//根據(jù)下標設(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));
		}
	}
}

運行結(jié)果

JavaBean與json字符串互轉(zhuǎn)

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對象轉(zhuǎn)json字符串

//定義對象
Student stu = new Student("張三", "123456");
//JavaBean對象轉(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對象
JSONObject json = JSONObject.fromObject(jsondata);
//轉(zhuǎn)為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對象轉(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對象
		JSONObject json = JSONObject.fromObject(jsondata);
		//轉(zhuǎn)為JavaBean對象
		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);
	}
}

運行結(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()) {
	//取出一個字符串對象
	String key = (String)ite.next();
	//轉(zhuǎn)為json格式
	JSONObject jsonObject = JSONObject.fromObject(map2.get(key));
	//轉(zhuǎn)為對象
	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()) {
			//取出一個字符串對象
			String key = (String)ite.next();
			//轉(zhuǎn)為json格式
			JSONObject jsonObject = JSONObject.fromObject(map2.get(key));
			//轉(zhuǎn)為對象
			Student stu = (Student)JSONObject.toBean(jsonObject, Student.class);
			System.out.println(key+"  "+stu);
		}
	}
}

運行結(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是用的上面的那個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);
		}
	}
}

運行結(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");
		}
	}
}

運行結(jié)果

PS:關(guān)于json操作,這里再為大家推薦幾款比較實用的json在線工具供大家參考使用:

在線JSON代碼檢驗、檢驗、美化、格式化工具:
http://tools.jb51.net/code/json

JSON在線格式化工具:
http://tools.jb51.net/code/jsonformat

在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson

json代碼在線格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.jb51.net/code/jsoncodeformat

在線json壓縮/轉(zhuǎn)義工具:
http://tools.jb51.net/code/json_yasuo_trans

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java操作json格式數(shù)據(jù)技巧總結(jié)》、《Java數(shù)組操作技巧總結(jié)》、《Java字符與字符串操作技巧總結(jié)》、《Java數(shù)學(xué)運算技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》及《Java操作DOM節(jié)點技巧總結(jié)

希望本文所述對大家java程序設(shè)計有所幫助。

相關(guān)文章

  • Java中的static--靜態(tài)變量你了解嗎

    Java中的static--靜態(tài)變量你了解嗎

    Java 中被 static 修飾的成員稱為靜態(tài)成員或類成員。它屬于整個類所有,而不是某個對象所有,即被類的所有對象所共享。靜態(tài)成員可以使用類名直接訪問,也可以使用對象名進行訪問,.下面我們來詳細了解一下吧
    2021-09-09
  • Java之Error與Exception的區(qū)別案例詳解

    Java之Error與Exception的區(qū)別案例詳解

    這篇文章主要介紹了Java之Error與Exception的區(qū)別案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • Java報錯:找不到或無法加載主類的解決辦法

    Java報錯:找不到或無法加載主類的解決辦法

    在Java中當(dāng)您嘗試運行一個類作為主類時,如果系統(tǒng)找不到該類或者無法加載該類,就會出現(xiàn)"找不到或無法加載主類"的錯誤,這篇文章主要給大家介紹了關(guān)于Java報錯:找不到或無法加載主類的解決辦法,需要的朋友可以參考下
    2024-12-12
  • 搭建 springboot selenium 網(wǎng)頁文件轉(zhuǎn)圖片環(huán)境的詳細教程

    搭建 springboot selenium 網(wǎng)頁文件轉(zhuǎn)圖片環(huán)境的詳細教程

    這篇文章主要介紹了搭建 springboot selenium 網(wǎng)頁文件轉(zhuǎn)圖片環(huán)境,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • 解決java頁面URL地址傳輸參數(shù)亂碼的方法

    解決java頁面URL地址傳輸參數(shù)亂碼的方法

    這篇文章主要介紹了解決java頁面URL地址傳輸參數(shù)亂碼的方法,URL地址參數(shù)亂碼問題,算是老話重談了吧!需要的朋友可以參考下
    2015-09-09
  • Java中如何使用?byte?數(shù)組作為?Map?的?key

    Java中如何使用?byte?數(shù)組作為?Map?的?key

    本文將討論在使用HashMap時,當(dāng)byte數(shù)組作為key時所遇到的問題及其解決方案,介紹使用String和List這兩種數(shù)據(jù)結(jié)構(gòu)作為臨時解決方案的方法,感興趣的朋友跟隨小編一起看看吧
    2023-06-06
  • IDEA熱部署配置詳細教程

    IDEA熱部署配置詳細教程

    這篇文章主要介紹了IDEA熱部署配置詳細教程,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • java之抽象類和繼承抽象類解讀

    java之抽象類和繼承抽象類解讀

    這篇文章主要介紹了java之抽象類和繼承抽象類,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Spring Boot開發(fā)Web應(yīng)用詳解

    Spring Boot開發(fā)Web應(yīng)用詳解

    這篇文章主要介紹了Spring Boot開發(fā)Web應(yīng)用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • spring boot實戰(zhàn)之本地jar包引用示例

    spring boot實戰(zhàn)之本地jar包引用示例

    本篇文章主要介紹了spring boot實戰(zhàn)之本地jar包引用示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10

最新評論

旌德县| 陈巴尔虎旗| 临安市| 婺源县| 昆明市| 库尔勒市| 安化县| 北碚区| 定结县| 临夏市| 外汇| 墨江| 信宜市| 石首市| 江油市| 钟祥市| 嘉善县| 龙井市| 牡丹江市| 乳山市| 宿州市| 桐柏县| 临沂市| 科技| 博湖县| 山东省| 阿城市| 通渭县| 桐柏县| 康乐县| 长乐市| 甘泉县| 敦化市| 会理县| 鄂尔多斯市| 舒城县| 丰宁| 田林县| 荃湾区| 泉州市| 凌云县|