$Java-json系列(二):用JSONObject解析和处理json数据
本文中主要介绍JSONObject处理json数据时候的一些常用场景和方法。
(一)jar包下载
所需jar包打包下载百度网盘地址:https://pan.baidu.com/s/1c27Uyre
(二)常见场景及处理方法
1、解析简单的json字符串:
// 简单的json测试字符串
public static final String JSON_SIMPLE = "{'name':'tom','age':16}"; JSONObject obj = JSONObject.fromObject(JSON_SIMPLE);
System.out.println("name is : " + obj.get("name"));
System.out.println("age is : " + obj.get("age"));
输出:
name is : tom
age is : 16
2、解析嵌套的json字符串:
// 嵌套的json字符串
public static final String JSON_MULTI = "{'name':'tom','score':{'Math':98,'English':90}}";
JSONObject obj = JSONObject.fromObject(JSON_MULTI);
System.out.println("name is : " + obj.get("name"));
System.out.println("score is : " + obj.get("score")); JSONObject scoreObj = (JSONObject) obj.get("score");
System.out.println("Math score is : " + scoreObj.get("Math"));
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类,否则会转换异常)
public class Person {
private String name;
private Info info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Info getInfo() {
return info;
}
public void setInfo(Info info) {
this.info = info;
}
@Override
public String toString() {
return "Person [name=" + name + ", info=" + info + "]";
}
}
public class Info {
private int age;
private Score score;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Score getScore() {
return score;
}
public void setScore(Score score) {
this.score = score;
}
@Override
public String toString() {
return "Info [age=" + age + ", score=" + score + "]";
}
}
public class Score {
private String math;
private String english;
public String getMath() {
return math;
}
public void setMath(String math) {
this.math = math;
}
public String getEnglish() {
return english;
}
public void setEnglish(String english) {
this.english = english;
}
@Override
public String toString() {
return "Score [math=" + math + ", english=" + english + "]";
}
}
转换方法:
Score score = new Score();
score.setEnglish("A");
score.setMath("B"); Info info = new Info();
info.setAge(20);
info.setScore(score); Person person = new Person();
person.setInfo(info);
person.setName("Tim"); JSONObject obj = JSONObject.fromObject(person);
System.out.println(obj.toString());
输出:
{
"name": "Tim",
"info": {
"score": {
"english": "A",
"math": "B"
},
"age": 20
}
}
4、把json数组转换成JsonObject数组:
// 数组形式的json
public static final String JSON_ARRAY = "[{'name':'tom'},{'name':'john','age':20},{}]"; JSONArray arr = JSONArray.fromObject(JSON_ARRAY);
System.out.println(arr); for (int i = 0; i < arr.size(); i++) {
JSONObject obj = arr.getJSONObject(i);
System.out.println(obj.toString());
}
输出:
[{"name":"tom"},{"name":"john","age":20},{}]
{"name":"tom"}
{"name":"john","age":20}
{}
5、构造一个json字符串:
JSONObject obj = new JSONObject();
obj.put("name", "tom");
obj.put("age", 19); // 子对象
JSONObject objContact = new JSONObject();
objContact.put("tel", "123456");
objContact.put("email", "tom@test.com");
obj.put("contact", objContact); // 子数组对象
JSONArray scoreArr = new JSONArray();
JSONObject objEnglish = new JSONObject();
objEnglish.put("course", "english");
objEnglish.put("result", 100);
objEnglish.put("level", "A"); JSONObject objMath = new JSONObject();
objMath.put("course", "math");
objMath.put("result", 50);
objMath.put("level", "D"); scoreArr.add(objEnglish);
scoreArr.add(objMath); obj.put("score", scoreArr); 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中的字段的顺序有没有办法设置?
随机推荐
- EasyUI DataGrid 相同连续列合并
扩展方法:$.extend($.fn.datagrid.methods, { autoMergeCells: function(jq, fields) { return jq.each(functio ...
- Spring MVC内部资源视图解析器
InternalResourceViewResolver用于将提供的URI解析为实际URI.下面的示例演示如何在Spring Web MVC框架中使用SpringResultViewResolver. ...
- android模拟器怎么直接安装apk
方法一:进入adb的tools目录,下面有adb.exe如图, 在cmd下进入tools目录,然后输入adb install apk路径,就行了 方法二:直接写一个批处理文件 运行就行了.
- go http的三种实现---3
package main //效率最高的一个方法 import ( "fmt" "io" "log" "net/http" ...
- Tomcat工作原理解析!
Tomcat简介 作者:杨晓(http://blog.sina.com.cn/u/1237288325) 一.Tomcat背景 自从JSP发布之后,推出了各式各样的JSP引擎.Apache Gro ...
- iOS开发笔记--如何实现程序长时间未操作退出
我们使用金融软件经常会发现手机锁屏或者长时间未操作就会退出程序或者需要重新输入密码等情况.下面让我们看一下如何实现这种功能.我们知道iOS有一个事件循环机制,也就是大家所说的runloop.我们在对程 ...
- Unity3d 多次显示关闭一个UI
publicclass OpenClooseGoUI : MonoBehaviour { public GameObject closeBt; public GameObject goUI ...
- JLable设置复制粘贴
final JLabel keyLable = new JLabel(key); keyLable.addMouseListener(new MouseAdapter() { @Override pu ...
- Python3.6全栈开发实例[021]
21.给出一个纯数字列表. 请对列表进行排序(升级题). 思路: (1)完成a和b的数据交换. 例如, a = 10, b = 24 交换之后, a = 24, b = 10(2)循环列表. 判断a[ ...
- Python3.6全栈开发实例[012]
12.输出商品列表,用户输入序号,显示用户选中的商品(升级题) 商品列表: goods = [{"name": "电脑", "price": ...