Gson parses JSON

Source: Internet
Author: User

Encounter [

{"ID": "6", "catid": "0", "title": "\ u5c0f \ u767d", "style": "", "Thumb ": "", "keywords": "", "Description": "", "posids": "0", "url": "index. PHP? M = YP & C = index & A = show & catid = & id = 6 "," listorder ":" 0 "," status ":" 99 "," userid ": "1", "username": "zhanghao", "inputtime": "1349156280", "updatetime": "1349156280", "Elite": "0", "name_py ": "Xiaobai", "class_id": "2", "class": "\ u5c0f \ u7eff \ u73ed", "bj_id": "2", "cou_id ": [{"title": "\ u5218 \ u6052 \ u5fd7" },{ "title": "\ u5f20 \ u6d69" },{ "title ": "\ u674e \ u5c0f \ u9f99" },{ "title": "\ u5c0f \ u864e"}]},

{"Id": "5", "catid": "0", "title": "\ u5c0f \ u7ea2", "style": "", "thumb ": "", "keywords": "", "description": "", "posids": "0", "url": "index. php? M = yp & c = index & a = show & catid = & id = 5 "," listorder ":" 0 "," status ":" 99 "," userid ": "1", "username": "zhanghao", "inputtime": "1349156249", "updatetime": "1349156249", "elite": "0", "name_py ": "xiaohong", "class_id": "2", "class": "\ u5c0f \ u7eff \ u73ed", "bj_id": "2", "cou_id ": [{"title": "\ u5218 \ u6052 \ u5fd7" },{ "title": "\ u5f20 \ u6d69" },{ "title ": "\ u674e \ u5c0f \ u9f99" },{ "title": "\ u5c0f \ u864e"}]

Fields contain arrays. Note the following when parsing with gson:

The brown mark is A large object (marked as class A), and the red mark can be marked as an object class B. In this way, when declaring A, List <B> cou_id; it is a member variable, String title; it is a member variable of B and can be parsed by gson. Take a look at the Code:

Type listType = new TypeToken <shortlist <Baby> (){}. getType (); Gson gson = new Gson (); sort list <Baby> babys = gson. fromJson (jsonData, listType); // The User object will be saved to the User list System. out. println (babys. size (); for (Baby baby: babys) {System. out. println (baby );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.