JSONObject jsonObject = JsonTools.getJsonObject("person", person); // 通过JSONObject的toBean方法可以将json对象转换成一个javabean JSONObject personObject = jsonObject.getJSONObject("person"); Person person2 = (Person) JSONObject.toBean(personObject, Person.class); System.out.println(person2);
Person [id=1, name=xiaoluo, address=广州]
②转换List<Person>类型的对象
@Test public void testPersonsJson() { List<Person> persons = new ArrayList<Person>(); Person person = new Person(1, "xiaoluo", "广州"); Person person2 = new Person(2, "android", "上海"); persons.add(person); persons.add(person2); String personsString = JsonTools.getJsonString("persons", persons); System.out.println(personsString); JSONObject jsonObject = JsonTools.getJsonObject("persons", persons); // List<Person>相当于一个JSONArray对象 JSONArray personsArray = (JSONArray)jsonObject.getJSONArray("persons"); List<Person> persons2 = (List<Person>) personsArray.toCollection(personsArray, Person.class); System.out.println(persons2); }
{"persons":[{"address":"广州","id":1,"name":"xiaoluo"},{"address":"上海","id":2,"name":"android"}]} [Person [id=1, name=xiaoluo, address=广州], Person [id=2, name=android, address=上海]]
③List<Map<String, String>>类型的json对象转换
@Test public void testMapJson() { List<Map<String, String>> list = new ArrayList<Map<String, String>>(); Map<String, String> map1 = new HashMap<String, String>(); map1.put("id", "001"); map1.put("name", "xiaoluo"); map1.put("age", "20"); Map<String, String> map2 = new HashMap<String, String>(); map2.put("id", "002"); map2.put("name", "android"); map2.put("age", "33"); list.add(map1); list.add(map2); String listString = JsonTools.getJsonString("list", list); System.out.println(listString); JSONObject jsonObject = JsonTools.getJsonObject("list", list); JSONArray listArray = jsonObject.getJSONArray("list"); List<Map<String, String>> list2 = (List<Map<String, String>>) listArray.toCollection(listArray, Map.class); System.out.println(list2); }
{"list":[{"id":"001","age":"20","name":"xiaoluo"},{"id":"002","age":"33","name":"android"}]} [{id=001, name=xiaoluo, age=20}, {id=002, name=android, age=33}]
通过上面的例子,我们可以了解了如何通过json-lib这个解析库来实现javabean、List、Map等数据和json数据的互相转换
2.gson()
下面我们来看看Google提供的gson这个json解析库,同样我们需要去下载gson这个jar包,导入到我们的项目中
使用gson,我们可以非常轻松的实现数据对象和json对象的相互转化,其中我们最常用的就是两个方法,一个是fromJSON(),将json对象转换成我们需要的数据对象,另一个是toJSON(),这个就是将我们的数据对象转换成json对象。下面我们也通过一个综合的例子来看看gson的使用方法:
public class JsonService { public Person getPerson() { Person person = new Person(1, "xiaoluo", "广州"); return person; } public List<Person> getPersons() { List<Person> persons = new ArrayList<Person>(); Person person = new Person(1, "xiaoluo", "广州"); Person person2 = new Person(2, "android", "上海"); persons.add(person); persons.add(person2); return persons; } public List<String> getString() { List<String> list = new ArrayList<String>(); list.add("广州"); list.add("上海"); list.add("北京"); return list; } public List<Map<String, String>> getMapList() { List<Map<String, String>> list = new ArrayList<Map<String, String>>(); Map<String, String> map1 = new HashMap<String, String>(); map1.put("id", "001"); map1.put("name", "xiaoluo"); map1.put("age", "20"); Map<String, String> map2 = new HashMap<String, String>(); map2.put("id", "002"); map2.put("name", "android"); map2.put("age", "33"); list.add(map1); list.add(map2); return list; } }
public static void main(String[] args) { Gson gson = new Gson(); JsonService jsonService = new JsonService(); Person person = jsonService.getPerson(); System.out.println("person: " + gson.toJson(person)); // 对于Object类型,使用 fromJson(String, Class)方法来将Json对象转换成Java对象 Person person2 = gson.fromJson(gson.toJson(person), Person.class); System.out.println(person2); System.out.println("------------------------------------------------"); List<Person> persons = jsonService.getPersons(); System.out.println("persons: " + gson.toJson(persons)); /* * 对于泛型对象,使用fromJson(String, Type)方法来将Json对象转换成对应的泛型对象 * new TypeToken<>(){}.getType()方法 */ List<Person> persons2 = gson.fromJson(gson.toJson(persons), new TypeToken<List<Person>>(){}.getType()); System.out.println(persons2); System.out.println("------------------------------------------------"); List<String> list = jsonService.getString(); System.out.println("String---->" + gson.toJson(list)); List<String> list2 = gson.fromJson(gson.toJson(list), new TypeToken<List<String>>(){}.getType()); System.out.println("list2---->" + list2); System.out.println("------------------------------------------------"); List<Map<String, String>> listMap = jsonService.getMapList(); System.out.println("Map---->" + gson.toJson(listMap)); List<Map<String, String>> listMap2 = gson.fromJson(gson.toJson(listMap), new TypeToken<List<Map<String, String>>>(){}.getType()); System.out.println("listMap2---->" + listMap2); System.out.println("------------------------------------------------"); }
看看控制台的输出: