JSON解析详解
三种方式:原生的JSON解析,Google的GSON解析,ali的FastJSON解析
一.原生的JSON解析
a) Json不存在数组
把获得数据封装成对象,若不存在数组,那么数据可以解析成对象
import org.json.JSONException;
import org.json.JSONObject;
public class Demo04 {
public static void main(String[] args) throws JSONException {
String json = "{name:'zhangsan',age:20}";
JSONObject object = new JSONObject(json);
String name = object.getString("name");
int age = object.getInt("age");
System.out.println("name" + name + "," + "age" + age);
}
}
b) Json只存在数组(使用集合容器储存数据)
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Demo01 {
public static void main(String[] args) throws JSONException {
List<Person> list = new ArrayList<>();
String json = "[{name:'zhangsan',age:20},{name:'lisi',age:21},{name:'wangwu',age:22}]";
JSONArray array = new JSONArray(json);
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
String name = object.getString("name");
int age = object.getInt("age");
Person p = new Person();
p.setName(name);
p.setAge(age);
list.add(p);
}
for (Person person : list) {
System.out.println(person);
}
}
}
c) Json既存在数组,也存在其他数据
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Demo02 {
public static void main(String[] args) throws JSONException {
Bumen bumen = new Bumen();
List<Employee> list = new ArrayList<>();
String json = "{no:1,name:'android',employees:[{name:'zhangsan',age:20},{name:'lisi',age:21},{name:'wangwu',age:22}]}";
JSONObject object = new JSONObject(json);
int no = object.getInt("no");
String name = object.getString("name");
bumen.setNo(no);
bumen.setName(name);
JSONArray array = new JSONArray(object.getString("employees"));
for (int i = 0; i < array.length(); i++) {
JSONObject object2 = array.getJSONObject(i);
String name2 = object2.getString("name");
int age = object2.getInt("age");
Employee employee = new Employee();
employee.setName(name2);
employee.setAge(age);
list.add(employee);
}
bumen.setList(list);
System.out.println(bumen.toString());
}
}
二.Google的GSON解析
a) 步骤
i. 不存在数组
Gson gson = new Gson();
Person person = gson.fromJSON(json字符串,Class<T>);
ii. 存在数组
Gson gson = new Gson();
//利用TypeToken进行反射定义类型
TypeToken<List<类型>> typetoken = new TypeToken<List<类型>>(){
};
List<类型> list = gson.fromJson(json,typetoken.getType());
b) 将对象封装成Json字符串
i.不存在数组
Gson gson = new Gson();
String string = gson.toGson(对象);
ii.存在数组
Gson gson = new Gson();
String s2 = gson.toJson(list);
C)例子
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class GsonDemo02 {
public static void main(String[] args) {
String json = "[{name:'zhangsan',age:20},{name:'lisi',age:21}," + "{name:'wangwu',age:22}]";
Gson gson = new Gson();
TypeToken<List<Person>> typeToken = new TypeToken<List<Person>>() {
};
List<Person> list = gson.fromJson(json, typeToken.getType());
for (Person p : list) {
System.out.println(p);
}
String s2 = gson.toJson(list);
System.out.println(s2);
}
}
person类:
class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
三.ALI的FASTJSON解析
例子:
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
public class Demo01 {
public static void main(String[] args) {
Person person = new Person("张三", 20);
String json = JSON.toJSONString(person);
System.out.println(json);
person = JSON.parseObject(json, Person.class);
System.out.println(person.toString());
Person person2 = new Person("李四", 21);
List<Person> list = new ArrayList<>();
list.add(person);
list.add(person2);
String jsonString = JSON.toJSONString(list);
System.out.println(jsonString);
list = JSON.parseArray(jsonString, Person.class);
System.out.println(list);
}
}