Java-json系列(二):用JSONObject解析和处理json数据
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java-json系列(二):用JSONObject解析和处理json数据,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4220字,纯文字阅读大概需要7分钟。
内容图文
本文中主要介绍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}"; 34 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")); 67 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 } 1314public Info getInfo() { 15return info; 16 } 1718publicvoid setInfo(Info info) { 19this.info = info; 20 } 2122 @Override 23public String toString() { 24return "Person [name=" + name + ", info=" + info + "]"; 25 } 2627 }
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) { 10this.age = age; 11 } 1213public Score getScore() { 14return score; 15 } 1617publicvoid setScore(Score score) { 18this.score = score; 19 } 2021 @Override 22public String toString() { 23return "Info [age=" + age + ", score=" + score + "]"; 24 } 2526 }
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 } 1213public String getEnglish() { 14return english; 15 } 1617publicvoid setEnglish(String english) { 18this.english = english; 19 } 2021 @Override 22public String toString() { 23return "Score [math=" + math + ", english=" + english + "]"; 24 } 2526 }
转换方法:
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"); 1213 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 7for (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); 1011// 子数组对象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"); 1718 JSONObject objMath = new JSONObject(); 19 objMath.put("course", "math"); 20 objMath.put("result", 50); 21 objMath.put("level", "D"); 2223 scoreArr.add(objEnglish); 24 scoreArr.add(objMath); 2526 obj.put("score", scoreArr); 2728 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中的字段的顺序有没有办法设置?
原文:http://www.cnblogs.com/jiayongji/p/6417862.html
内容总结
以上是互联网集市为您收集整理的Java-json系列(二):用JSONObject解析和处理json数据全部内容,希望文章能够帮你解决Java-json系列(二):用JSONObject解析和处理json数据所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。