public class JsonTools {
public JsonTools() {
}
// {"persons":[{"id":1001,"address":"China","name":"cat"},{"id":1002,"address":"Japan","name":"dog"}]}
public static JSONObject createArrayJson(List<Person> lists) {
JSONObject persons = new JSONObject();
JSONArray array = new JSONArray();
try {
for (Person list : lists) {
JSONObject person = new JSONObject();
person.put("id", list.getId());
person.put("name", list.getName());
person.put("address", list.getAddress());
array.put(person);
}
persons.put("persons", array);
} catch (JSONException e) {
e.printStackTrace();
}
return persons;
}
public static List<Person> analysisArrayJson(String json) {
List<Person> lists = new ArrayList<Person>();
JSONObject jo = null;
try {
jo = new JSONObject(json);
} catch (JSONException e) {
e.printStackTrace();
Log.e("gg", e.getMessage(), e);
}
if (jo == null) {
return lists;
}
JSONArray ja = jo.optJSONArray("persons");
if (ja != null) {
for (int i = 0; i < ja.length(); i++) {
JSONObject obj = ja.optJSONObject(i);
if (obj == null) {
continue;
}
Person person = new Person();
person.setId(obj.optInt("id"));
person.setName(obj.optString("name"));
person.setAddress(obj.optString("address"));
lists.add(person);
}
}
return lists;
}
// {"id":"1","name":"李磊","age":"30"}
public static String createSimpleJson() {
JSONObject jo = new JSONObject();
try {
jo.put("id", 1);
jo.put("name", "李磊");
jo.put("age", 30);
} catch (JSONException e) {
e.printStackTrace();
}
return jo.toString();
}
public static void analysisSmipleJson(String json) {
JSONObject jo = null;
try {
jo = new JSONObject(json);
} catch (JSONException e) {
e.printStackTrace();
}
if (jo == null) {
return;
}
int age = jo.optInt("age");
String name = jo.optString("name");
int id = jo.optInt("id");
id++;
}
// 假设现在要创建这样一个json文本
// {
// "id" : "1", // 字符串
// "phone" : ["110", "120"], // 数组
// "age" : 100, // 数值
// "address" : { "country" : "Chian", "city" : "Beijing" }, // 对象
// "married" : false // 布尔值
// }
public static String createcomplexJson() {
JSONObject jo = new JSONObject();
try {
jo.put("id", "1");
JSONArray array = new JSONArray();
array.put("110");
array.put("120");
jo.put("phone", array);
jo.put("age", 30);
JSONObject address = new JSONObject();
address.put("country", "China");
address.put("city", "Beijing");
jo.put("address", address);
jo.put("married", false);
} catch (JSONException e) {
e.printStackTrace();
}
return jo.toString();
}
public static void analysisComplexJson(String json) {
JSONObject jo = null;
try {
jo = new JSONObject(json);
} catch (JSONException e) {
e.printStackTrace();
}
if (jo == null) {
return;
}
String id = jo.optString("id");
JSONArray ja = jo.optJSONArray("phone");
if (ja != null) {
for (int i = 0; i < ja.length(); i++) {
String phone = ja.optString(i);
}
}
int age;
// try {
// age = jo.getInt("agea");
// } catch (JSONException e) {
// age = 0;
// e.printStackTrace();
// }
age = jo.optInt("age"); // 如果有错,有默认值:0
JSONObject address = jo.optJSONObject("address");
if (address != null) {
String country = address.optString("country");
String city = address.optString("city");
}
boolean married = jo.optBoolean("married");
}
}