JSON

JSONÊý¾Ý½âÎöµÄʵÀý(2)

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2017-04-14 12:03 ÎÒÒªÆÀÂÛ( )

@Test public void testPersonsJson() {ListPerson persons = new ArrayListPerson();Person person = new Person(1, "xiaoluo", "¹ãÖÝ");Person person2 = new Person(2, "android", "ÉϺ£");persons.add(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£¬ÎÒÃÇ¿ÉÒԷ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("------------------------------------------------"); }

¿´¿´¿ØÖÆ̨µÄÊä³ö£º

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • Android¿ª·¢£ºVideoView²¥·ÅÒôÀÖʵÀý

    Android¿ª·¢£ºVideoView²¥·ÅÒôÀÖʵÀý

    2017-03-07 12:00

  • android_json½Ì³Ì

    android_json½Ì³Ì

    2016-11-18 17:01

  • JSON½âÎöºÍXML½âÎö¶Ô±È£¬JSON½âÎöXML½âÎö

    JSON½âÎöºÍXML½âÎö¶Ô±È£¬JSON½âÎöXML½âÎö

    2016-10-21 11:01

  • AndroidÖ®JSONÈ«Ãæ½âÎöÓëʹÓÃ

    AndroidÖ®JSONÈ«Ãæ½âÎöÓëʹÓÃ

    2016-06-11 12:00

ÍøÓѵãÆÀ
¹