1.1 JSON字符串取得单个key
使用方法: object.get(Object key) 或者 object.getString(String key)
String content = "{\"name\" : \"jack\"}";
try {
JSONObject object = JSONObject.parseObject(content);
String name = (String) object.get("name"); //jack
} catch (Exception e) {
}
String content = "{\"name\" : \"jack\"}";
try {
JSONObject object = JSONObject.parseObject(content);
String name2 = object.getString("name"); //jack
} catch (Exception e) {
}
1.2 JSON字符串获取实体类
使用方法: JSON.parseObject(String text, Class<T> clazz)
import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
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 ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
String content = "{\"data\" : {\"name\" : \"jack\", \"age\" : 18}}";
try {
JSONObject object = JSONObject.parseObject(content);
JSONObject json = object.getJSONObject("data");
Student student = JSONObject.parseObject(json.toString(), Student.class);
System.out.println(student); // Student[name=jack,age=18]
} catch (Exception e) {
}
1.3 JSON字符串获取List实体类
使用方法: JSONArray.parseArray(String text, Class<T> clazz) 或者 JSON.parseArray(String text, Class<T> clazz)
String content = "{\"data\" : [{\"name\" : \"jack\", \"age\" : 18},{\"name\" : \"jack\", \"age\" : 18}]}";
try {
JSONObject object = JSONObject.parseObject(content);
Object objStudent = object.get("data");
List<Student> studentArray = JSONArray.parseArray(objStudent.toString(), Student.class);
System.out.println(Arrays.toString(studentArray.toArray())); // [Student[name=jack,age=18], Student[name=jack,age=18]]
} catch (Exception e) {
}
String content = "{\"data\" : [{\"name\" : \"jack\", \"age\" : 18},{\"name\" : \"jack\", \"age\" : 18}]}";
try {
JSONObject object = JSONObject.parseObject(content);
Object objStudent = object.get("data");
List<Student> studentArray = JSON.parseArray(objStudent.toString(),Student.class); //
System.out.println(Arrays.toString(studentArray.toArray())); // [Student[name=jack,age=18], Student[name=jack,age=18]]
} catch (Exception e) {
}
1.3.1 JSON字符串获取List实体类一层通用方法
public static <T> List<T> parseSyncResult(String result, Class<T> clazz){
try {
JSONObject jsonObject = JSONObject.parseObject(result);
String data = jsonObject.getString("data");// data 根据实际情况调整
return JSON.parseArray(data, clazz);
} catch (Exception e) {
return Collections.emptyList();
}
}
例子:
String content = "{\"data\" : [{\"name\" : \"jack\", \"age\" : 18},{\"name\" : \"jack\", \"age\" : 18}]}";
List<Student> studentArray = parseSyncResult(content,Student.class);
System.out.println(Arrays.toString(studentArray.toArray())); // [Student[name=jack,age=18], Student[name=jack,age=18]]
1.3.2 JSON字符串获取List实体类两层通用方法
public static <T> List<T> parseSyncResult(String result, Class<T> clazz) throws Exception{
JSONObject jsonObject = JSONObject.parseObject(result);
JSONArray jsonArray = jsonObject.getJSONObject("content").getJSONArray("data");// content和data根据实际情况调整
return JSON.parseArray(jsonArray.toJSONString(), clazz);
}
例子:
String content = "{\"content\":{\"total\":2,\"data\":[{\"name\" : \"jack\", \"age\" : 18},{\"name\" : \"jack\", \"age\" : 18}]},\"msgs\":\"\",\"status\":200}";
List<Student> studentArray2 = parseSyncResult(content, Student.class);
System.out.println(Arrays.toString(studentArray2.toArray())); // [Student[name=jack,age=18], Student[name=jack,age=18]]