@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£¬ÎÒÃÇ¿ÉÒԷdz£ÇáËɵÄʵÏÖÊý¾Ý¶ÔÏóºÍ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("------------------------------------------------"); }
¿´¿´¿ØÖÆ̨µÄÊä³ö£º
¡¡