一、JSON数据类型
1、JSON对象:
JSON 对象在 “{}”中,对象中可以包含多个键值对。
2、JSON数组:
JSON 数组在中括号中书写,数组中包含多个对象。
二、Java对json的操作:
1、构建JSON:
package com.pbm.test;
import java.util.ArrayList;
import java.util.HashMap;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JsonTest {
public static void main(String[] args) {
try {
//JsonObject和JsonArray区别就是JsonObject是对象形式,JsonArray是数组形式
//创建对象形式JsonObject第一种方法
JSONObject jsonObject = new JSONObject();
jsonObject.put("UserName", "aa");
jsonObject.put("age", "21");
jsonObject.put("school", "西大");
System.out.println("对象形式:" + jsonObject);
//创建对象形式JsonObject第二种方法
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("UserName", "bb");
hashMap.put("age", "21");
hashMap.put("school", "科大");
System.out.println("对象形式:" + JSONObject.fromObject(hashMap));
//创建一个数组形式JsonArray方法1
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "cc");
jsonArray.add(1, "22");
jsonArray.add(2, "北大");
System.out.println("数组形式:" + jsonArray);
//创建数组形式JsonArray方法2
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("dd");
arrayList.add("22");
arrayList.add("清华");
System.out.println("数组形式:" + JSONArray.fromObject(arrayList));
//如果JSONArray解析一个HashMap,则会将整个对象的放进一个数组的值中
System.out.println("数组中包含对象:" + JSONArray.fromObject(hashMap));
//组装一个复杂的JSONArray
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("UserName", "ee");
jsonObject2.put("age", "22");
jsonObject2.put("school", "SDUST");
jsonObject2.element("students", arrayList);
System.out.println("对象中有数组:" + jsonObject2);
}catch(Exception e) {
e.printStackTrace();
}
}
}
输出结果
对象形式:{"UserName":"aa","age":"21","school":"西大"}
对象形式:{"UserName":"bb","school":"科大","age":"21"}
数组形式:["cc","22","北大"]
数组形式:["dd","22","清华"]
数组中包含对象:[{"UserName":"bb","school":"科大","age":"21"}]
对象中有数组:{"UserName":"ee","age":"22","school":"SDUST","students":["dd","22","清华"]}
2、JSON和对象相互转换:
(1)对象转json:
/*
*第一种:JSONObject.fromObject,将类对象转换为json对象,
*第二种:JSONArray.fromObject(),将类对象转化为json数组。
*/
JSONObject.fromObject()
JSONArray.fromObject()
先定义了一个类对象
package pers.xiaoma.model;
public class Student {
private String name;
private String age;
private String school;
public String getName() {
return this.name;
}
public void setName(String str) {
this.name = str;
}
public String getAge() {
return this.age;
}
public void setAge(String str) {
this.age = str;
}
public String getSchool() {
return this.school;
}
public void setSchool(String str) {
this.school = str;
}
public String toString() {
return "Student[name="+this.name+
",age="+this.age+",school"+this.school+"]";
}
}
下面,将=student转换为JSON格式:
package json;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import pers.xiaoma.model.Student;
public class ObjectToJson {
public static void main(String[] args) {
Student student = new Student();
student.setName("AA");
student.setAge("21");
student.setSchool("科大");
JSONObject json = JSONObject.fromObject(student);
JSONArray array = JSONArray.fromObject(student);
System.out.println("JsonObj: "+json);
System.out.println("JsonArray: "+array);
}
}
输出
JsonObj:{"name":"aa","age":"21","school":"科大"}
JsonArray:[{"name":"aa","age":"21","school":"科大"}]
(2)JSON字符串转为对象:
先把字符串转化为JSON对象,再将JSON对象转化为具体的类对象。
看一个例子,还是以student类为例:
package json;
import net.sf.json.JSONObject;
import pers.xiaoma.model.Student;
public class JsontoObj {
public static void main(String[] args) {
String jsonStr = "{\"age\":\"21\",\"name\":\"AA\",\"school\":\"科大\"}";
//先将字符串转化为json对象
JSONObject obj = JSONObject.fromObject(jsonStr);
//再将json对象转化为具体的类对象
Student student = (Student)JSONObject.toBean(obj,Student.class);
System.out.println("Student Name: "+student.getName());
System.out.println("Student Age: "+student.getAge());
System.out.println("Student School: "+student.getSchool());
}
}
(3)实体类转为json字符串
Student student = new Student();
student.setName("AA");
student.setAge("21");
student.setSchool("科大");
String jsonStr = JSONObject.toJSONString(student,true);