JSON data encapsulation and the use of Gson to encapsulate JSON data into beans

Source: Internet
Author: User

For the following JSON data:

Encapsulate using the following bean:

public class Newbean {public Newbeanitem data;public string Retcode;public class Newbeanitem{public string Countcommentur l;//Loading public string More;public string title;public list<news> news;public list<topic> topic;public List& Lt Topnews> topnews;} /** * Bottom ListView Display Entry JavaBean * @author Administrator * */public class News{public string Comment;public string Commentlis T;public string commenturl;//Uniqueness Flag Current News entry public string id;//News list Picture public string listimage;//time publicly string pubdate;// The title text of the News list public string Title;public string Type;public string url;//whether the news has read the flag bit public boolean isread;} public class Topic{public String Description;public string Id;public string listimage;public string sort;public string tit Le;public String URL;}  /** * Top Carousel Map * @author Administrator * */public class Topnews{public string Comment;public string Commentlist;public string Commenturl;public string id;//Carousel map Picture link Address public string topimage;//time publicly string pubdate;//News list title text publicly string title ;p ublic string type;public string URL;}} 

That is: Data is a bean, and [] represents a list collection

NOTE: null for JSON data returned by the server, the general processing is that if the field is null, the server is asked to return "" instead of NULL

Use Gson to parse the code as follows:

public class Gsonutil {/** * converts JSON string to JavaBean object * @param result * @param clazz * @return */public static <T> T JS Ontobean (String result,class<t> clazz) {Gson Gson = new Gson (); return Gson.fromjson (Result,clazz);}}

The tool class is called directly:

Newcenter = Gsonutil.jsontobean (result, newcenter.class);

JSON data encapsulation and the use of Gson to encapsulate JSON data into beans

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.