First, the resolution of simple JSON object
package com.point9.a_basic;
import org.json.JSONException;
import org.json.JSONObject;
import com.point9.b_entity.Student;
/**
*
* @author point9
* JSON解析:---方式一:基本的JSON解析
* 案例1:解析最外层为{}的简单对象的JSON数据
* 通过JsonObject获取
*/
public class Test01 {
public static void main(String[] args) throws JSONException {
String data = "{name:'zs',age:30}";
//最外层为{}的简单JSON对象用JsonObject解析
JSONObject jb = new JSONObject(data);
//取出数据并存储到对应的实体类中
String name = jb.getString("name");
int age = jb.getInt("age");
//System.out.println(name+"----"+age);//测试取出的数据
Student st = new Student(name,age);
System.out.println(st.toString());
}
}
Two JSON data, parsing outermost} {complex (nested) object of
package com.point9.a_basic;
import org.json.JSONException;
import org.json.JSONObject;
import com.point9.b_entity.Score;
import com.point9.b_entity.Student;
/**
*
* @author point9
* JSON解析:---方式一:基本的JSON解析
* 案例2:解析最外层为{}的复杂(有嵌套的)对象的JSON数据
* 通过JsonObject获取
*/
public class Test02 {
public static void main(String[] args) throws JSONException {
String data = "{name:'zs',age:30,score:{english:80,chinese:85}}";
JSONObject st = new JSONObject(data);
String name = st.getString("name");
int age = st.getInt("age");
//内层嵌套的JSON对象通过getJSONObject()获取
JSONObject sc = st.getJSONObject("score");
int english = sc.getInt("english");
int chinese = sc.getInt("chinese");
//将数据存储到对应的实体类中
Student student = new Student(name, age, new Score(english, chinese));
System.out.println(student.toString());
}
}
Three JSON data, parsing the outermost layer [] complex (with nested) arrays
package com.point9.a_basic;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.point9.b_entity.Score;
import com.point9.b_entity.Student;
/**
*
* @author point9
* JSON解析:---方式一:基本的JSON解析
* 案例3:解析最外层为[]的复杂(有嵌套的)数组的JSON数据
* 通过JsonArray解析
*/
public class Test03 {
public static void main(String[] args) throws JSONException {
String data = "[{name:'zs',age:30,score:{english:80,chinese:85}},{name:'ls',age:40,score:{english:59,chinese:89}}]";
JSONArray array = new JSONArray(data);
//数组的长度array.length()
for (int i = 0; i < array.length(); i++) {
//根据下标获取jsonObject对象
JSONObject st = array.getJSONObject(i);
String name = st.getString("name");
int age = st.getInt("age");
//Student student = new Student(name, age);
//通过getJSONObject()获得JSON对象中嵌套的JSON对象
JSONObject sc = st.getJSONObject("score");
int english = sc.getInt("english");
int chinese = sc.getInt("chinese");
//student.setScroe(new Score(english, chinese));
Student student = new Student(name, age, new Score(english, chinese));
System.out.println(student);
}
}
}
Fourth, the known objects, generating data JSON
package com.point9.a_basic;
import org.json.JSONObject;
import com.point9.b_entity.Score;
import com.point9.b_entity.Student;
/**
*
* @author point9
* JSON解析:---方式一:基本的JSON解析
* 案例4:已知对象,生成JSON数据
* JsonObject
*/
public class Test04 {
public static void main(String[] args) {
//实例化对象
Student st = new Student("Point9", 22,new Score(66, 88));
//传入对象,生成json数据
JSONObject jb = new JSONObject(st);
String data = jb.toString();
System.out.println(data);
}
}
Fifth, known array or collection, data generated json
package com.lmw.a_basic;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import com.point9.b_entity.Score;
import com.point9.b_entity.Student;
/**
*
* @author point9
* JSON解析:---方式一:基本的JSON解析
* 案例5: 已知数组或集合,生成json数据----JsonArray
*/
public class Test05 {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
Student st1 = new Student("zs", 20, new Score(60, 70));
Student st2 = new Student("ls", 30, new Score(68, 72));
list.add(st1);
list.add(st2);
//传入集合,生成json数组
JSONArray array = new JSONArray(list);
System.out.println(array.toString());
}
}
This paper required jar package: https://download.csdn.net/download/point9/10870403
Web full-stack technology exchange
Click on the link to join a group chat full stack exchange group [Web]: https://jq.qq.com/?_wv=1027&k=5rnUzsF