日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

$Java-json系列(二):用JSONObject解析和处理json数据

發布時間:2025/7/25 javascript 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 $Java-json系列(二):用JSONObject解析和处理json数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文中主要介紹JSONObject處理json數據時候的一些常用場景和方法。

(一)jar包下載

所需jar包打包下載百度網盤地址:https://pan.baidu.com/s/1c27Uyre

?

(二)常見場景及處理方法

1、解析簡單的json字符串:

1      // 簡單的json測試字符串 2 public static final String JSON_SIMPLE = "{'name':'tom','age':16}"; 3 4 JSONObject obj = JSONObject.fromObject(JSON_SIMPLE); 5 System.out.println("name is : " + obj.get("name")); 6 System.out.println("age is : " + obj.get("age"));

輸出:

name is : tom
age is : 16

?

2、解析嵌套的json字符串:

1      // 嵌套的json字符串 2 public static final String JSON_MULTI = "{'name':'tom','score':{'Math':98,'English':90}}"; 3 JSONObject obj = JSONObject.fromObject(JSON_MULTI); 4 System.out.println("name is : " + obj.get("name")); 5 System.out.println("score is : " + obj.get("score")); 6 7 JSONObject scoreObj = (JSONObject) obj.get("score"); 8 System.out.println("Math score is : " + scoreObj.get("Math")); 9 System.out.println("English score is : " + scoreObj.get("English"));

輸出:

name is : tom
score is : {"English":90,"Math":98}
Math score is : 98
English score is : 90

?

3、把bean對象轉化成JSONObject對象:

Person、Info、Score類分別如下:(注:要定義成獨立的三個public類,不能定義成內部類或非public類,否則會轉換異常)

1 public class Person { 2 private String name; 3 4 private Info info; 5 6 public String getName() { 7 return name; 8 } 9 10 public void setName(String name) { 11 this.name = name; 12 } 13 14 public Info getInfo() { 15 return info; 16 } 17 18 public void setInfo(Info info) { 19 this.info = info; 20 } 21 22 @Override 23 public String toString() { 24 return "Person [name=" + name + ", info=" + info + "]"; 25 } 26 27 } 1 public class Info { 2 private int age; 3 private Score score; 4 5 public int getAge() { 6 return age; 7 } 8 9 public void setAge(int age) { 10 this.age = age; 11 } 12 13 public Score getScore() { 14 return score; 15 } 16 17 public void setScore(Score score) { 18 this.score = score; 19 } 20 21 @Override 22 public String toString() { 23 return "Info [age=" + age + ", score=" + score + "]"; 24 } 25 26 } 1 public class Score { 2 private String math; 3 private String english; 4 5 public String getMath() { 6 return math; 7 } 8 9 public void setMath(String math) { 10 this.math = math; 11 } 12 13 public String getEnglish() { 14 return english; 15 } 16 17 public void setEnglish(String english) { 18 this.english = english; 19 } 20 21 @Override 22 public String toString() { 23 return "Score [math=" + math + ", english=" + english + "]"; 24 } 25 26 }

轉換方法:

1 Score score = new Score(); 2 score.setEnglish("A"); 3 score.setMath("B"); 4 5 Info info = new Info(); 6 info.setAge(20); 7 info.setScore(score); 8 9 Person person = new Person(); 10 person.setInfo(info); 11 person.setName("Tim"); 12 13 JSONObject obj = JSONObject.fromObject(person); 14 System.out.println(obj.toString());

輸出:

?{
??? "name": "Tim",
??? "info": {
??????? "score": {
??????????? "english": "A",
??????????? "math": "B"
??????? },
??????? "age": 20
??? }
}

?

4、把json數組轉換成JsonObject數組:

1 // 數組形式的json 2 public static final String JSON_ARRAY = "[{'name':'tom'},{'name':'john','age':20},{}]"; 3 4 JSONArray arr = JSONArray.fromObject(JSON_ARRAY); 5 System.out.println(arr); 6 7 for (int i = 0; i < arr.size(); i++) { 8 JSONObject obj = arr.getJSONObject(i); 9 System.out.println(obj.toString()); 10 }

輸出:

[{"name":"tom"},{"name":"john","age":20},{}]
{"name":"tom"}
{"name":"john","age":20}
{}

?

5、構造一個json字符串:

1 JSONObject obj = new JSONObject(); 2 obj.put("name", "tom"); 3 obj.put("age", 19); 4 5 // 子對象 6 JSONObject objContact = new JSONObject(); 7 objContact.put("tel", "123456"); 8 objContact.put("email", "tom@test.com"); 9 obj.put("contact", objContact); 10 11 // 子數組對象 12 JSONArray scoreArr = new JSONArray(); 13 JSONObject objEnglish = new JSONObject(); 14 objEnglish.put("course", "english"); 15 objEnglish.put("result", 100); 16 objEnglish.put("level", "A"); 17 18 JSONObject objMath = new JSONObject(); 19 objMath.put("course", "math"); 20 objMath.put("result", 50); 21 objMath.put("level", "D"); 22 23 scoreArr.add(objEnglish); 24 scoreArr.add(objMath); 25 26 obj.put("score", scoreArr); 27 28 System.out.println(obj.toString());

輸出:

{
??? "score": [
??????? {
??????????? "result": 100,
??????????? "level": "A",
??????????? "course": "english"
??????? },
??????? {
??????????? "result": 50,
??????????? "level": "D",
??????????? "course": "math"
??????? }
??? ],
??? "contact": {
??????? "tel": "123456",
??????? "email": "tom@test.com"
??? },
??? "name": "tom",
??? "age": 19
}

思考:輸出的json中的字段的順序有沒有辦法設置?

?

轉載于:https://www.cnblogs.com/jiayongji/p/6417862.html

總結

以上是生活随笔為你收集整理的$Java-json系列(二):用JSONObject解析和处理json数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。