android Json解析全
方式一
1、实体类
public class Person { private int age; private String name; public Person(int age, String name) { super(); this.age = age; this.name = name; } @Override public String toString() { return "Person [age=" + age + ", name=" + name + "]"; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Person() { // TODO Auto-generated constructor stub } }2、服务类
public class JsonList { public JsonList() { // TODO Auto-generated constructor stub } public static List<Person> getList() { List<Person> list = new ArrayList<Person>(); list.add(new Person(1002,"李四2")); list.add(new Person(1003,"李四3")); list.add(new Person(1001,"李四1")); return list; } public static Person getPerson(){ return new Person(1002,"李四2"); } }
import net.sf.json.JSON; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JSONSerializer; public class JsonTools { public JsonTools() { // TODO Auto-generated constructor stub } public static String getString(Person person) { JSON json = JSONSerializer.toJSON(JsonList.getPerson()); return json.toString(); } /** * * @param msgString * @return */ public static Person getPerson(String msgString) { Person person = new Person(); JSONArray array = new JSONArray(); array.add(msgString); JSONObject obj = array.getJSONObject(0); // System.out.println(obj.get("age")); // System.out.println(obj.get("name")); person.setAge(obj.getInt("age")); person.setName(obj.getString("name")); return person; } public static String getListString(List<Person> listPersons) { JSON json = JSONSerializer.toJSON(listPersons); return json.toString(); } /** * 将一个json格式的字符串转换成行对应的对象 * @param str * @return */ public static List<Person> getPersons(String str) { List<Person> list = new ArrayList<Person>(); JSONArray array = new JSONArray(); array.add(str); JSONArray array2 = array.getJSONArray(0); for (int i = 0; i <array2.size(); i++) { JSONObject jsonObject = array2.getJSONObject(i); Person person = new Person(); person.setAge(jsonObject.getInt("age")); person.setName(jsonObject.getString("name")); list.add(person); } return list; } }
1、实体类
public class Person { private int age; private String name; public Person(int age, String name) { super(); this.age = age; this.name = name; } @Override public String toString() { return "Person [age=" + age + ", name=" + name + "]"; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Person() { // TODO Auto-generated constructor stub } }
2、服务类
public class JsonList { public JsonList() { // TODO Auto-generated constructor stub } public static List<Person> getList() { List<Person> list = new ArrayList<Person>(); list.add(new Person(1002,"李四2")); list.add(new Person(1003,"李四3")); list.add(new Person(1001,"李四1")); return list; } public static Person getPerson(){ return new Person(1002,"李四2"); } }
import net.sf.json.JSON; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JSONSerializer; public class JsonTools { public JsonTools() { // TODO Auto-generated constructor stub } public static String getString(Person person) { JSON json = JSONSerializer.toJSON(JsonList.getPerson()); return json.toString(); } /** * * @param msgString * @return */ public static Person getPerson(String msgString) { Person person = new Person(); JSONArray array = new JSONArray(); array.add(msgString); JSONObject obj = array.getJSONObject(0); // System.out.println(obj.get("age")); // System.out.println(obj.get("name")); person.setAge(obj.getInt("age")); person.setName(obj.getString("name")); return person; } public static String getListString(List<Person> listPersons) { JSON json = JSONSerializer.toJSON(listPersons); return json.toString(); } /** * 将一个json格式的字符串转换成行对应的对象 * @param str * @return */ public static List<Person> getPersons(String str) { List<Person> list = new ArrayList<Person>(); JSONArray array = new JSONArray(); array.add(str); JSONArray array2 = array.getJSONArray(0); for (int i = 0; i <array2.size(); i++) { JSONObject jsonObject = array2.getJSONObject(i); Person person = new Person(); person.setAge(jsonObject.getInt("age")); person.setName(jsonObject.getString("name")); list.add(person); } return list; } }
方式三
1、实体类
public class Person { private String name; private int age; private String address; public String getName() { return name; } public Person(String name, int age, String address) { super(); this.name = name; this.age = age; this.address = address; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", address=" + address + "]"; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Person() { // TODO Auto-generated constructor stub } }
2、服务类
import com.android.domain.Person; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class GsonTools { public GsonTools() { // TODO Auto-generated constructor stub } public static String createGsonString(Object object) { Gson gson = new Gson(); String gsonString = gson.toJson(object); return gsonString; } public static <T> T changeGsonToBean(String gsonString, Class<T> cls) { Gson gson = new Gson(); T t = gson.fromJson(gsonString, cls); return t; } public static <T> List<T> changeGsonToList(String gsonString, Class<T> cls) { Gson gson = new Gson(); List<T> list_person = gson.fromJson(gsonString, new TypeToken<List<T>>() { }.getType()); return list_person; } public static List<Map<String, Object>> changeGsonToListMaps(String gsonString) { List<Map<String, Object>> list = null; Gson gson = new Gson(); list = gson.fromJson(gsonString, new TypeToken<List<Map<String, Object>>>() { }.getType()); return list; } }
方式四
1、实体类
public class Person { private String name; private int age; private String address; public String getName() { return name; } public Person(String name, int age, String address) { super(); this.name = name; this.age = age; this.address = address; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", address=" + address + "]"; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Person() { // TODO Auto-generated constructor stub } }
2、服务类
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.asm.Type; import com.alibaba.fastjson.util.TypeUtils; public class FastJsonTools { public FastJsonTools() { // TODO Auto-generated constructor stub } public static String createJsonString(Object object) { String jsonString = JSON.toJSONString(object); return jsonString; } public static <T> T createJsonBean(String jsonString, Class<T> cls) { T t = JSON.parseObject(jsonString, cls); return t; } public static <T> List<T> createJsonToListBean(String jsonString, Class<T> cls) { List<T> list = null; list = JSON.parseArray(jsonString, cls); return list; } /** * @param jsonString * @return */ public static List<Map<String, Object>> createJsonToListMap( String jsonString) { List<Map<String, Object>> list2 = JSON.parseObject(jsonString, new TypeReference<List<Map<String, Object>>>() { }); return list2; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // Person person1 = new Person("张三1", 24, "北京1"); // Person person2 = new Person("张三2", 23, "北京2"); // List<Person> list = new ArrayList<Person>(); // list.add(person1); // list.add(person2); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("name", "jack"); map.put("age", 23); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("name", "rose"); map2.put("age", 24); list.add(map); list.add(map2); String jsonString = JSON.toJSONString(list); System.out.println(jsonString); // JSON.parseArray(arg0, arg1) List<Map<String, Object>> list2 = JSON.parseObject(jsonString, new TypeReference<List<Map<String, Object>>>() { }); // List<Person> lists = JSON.parseArray(arg0, arg1); System.out.println(list2.toString()); } }