Android parsing JSON strings with Gson

Source: Internet
Author: User

There is a protected response<result<t>> parsenetworkresponse (Networkresponse Response) {} function in the volley framework. The JSON string obtained from the server or in the cache is parsed in this function.

New String (Response.data, Httpheaderparser.parsecharset (Response.headers, HTTP. utf_8)    result<T> result = Gsonutil.getresult (jsonstring, Mtypetoken);

We can define a Gsonutil class. It writes some functional functions that Gson and JSON convert to each other. For example, the GetResult function is used. Use this function to get the result<t> of this javabean. That is, the specification returned, which is negotiated by front-end developers and background developers. Now that I get the JavaBean, then I'm going to get the data in the JSON as simple as getting a data from a class.

 public  static  <T> T getresult (String jsondata, Typetoken<t> type) {Gson Gson  = new   Gsonbuilder (). Excludefieldswithoutexposeannotation (). Create ();  return   (T) Gson.fromjson (Jsondata,    Type.GetType ()); }

Result<t> class: For example: (the JSON string returned in the background such as: {"err": "Success", "errno": 1, "rsm{" UserId ":" 1 "," email ":"? "," Password ":"? "," FirstName ":" hu "," LastName ":" ann "," Gender ":" 1 "," Photo ":"/picture/userphoto/ 1422624411423.PNG "," Autograph ":" "," Introduce ":" "," Createtime ":" 4 days ago "}} failed to return {" Err ":" user does not exist ! ", errno": 0} )

 PackageWhu.cn.whushare.bean;ImportCom.google.gson.annotations.Expose;ImportCom.google.gson.annotations.SerializedName; Public classResult<t>{@Expose @SerializedName ("CurrentPage")    Private Longcurrentpage; @Expose @SerializedName ("PageSize")    Private LongpageSize; @Expose @SerializedName ("Alldatacount")    Private LongAlldatacount; @Expose @SerializedName ("NextPage")    Private LongNextPage; @Expose @SerializedName ("Errno")    Private intCode; @Expose @SerializedName ("Err")    PrivateString msg; Private intTotal ; @Expose @SerializedName ("RSM")    PrivateT content;  Publicresult (T result) { This. Content =result; }     PublicResult (intcode, T result) {         This. Code =Code;  This. Content =result; }     Public LongGetcurrentpage () {returncurrentpage; }     Public voidSetcurrentpage (intcurrentpage) {         This. currentpage =currentpage; }     Public Longgetpagesize () {returnpageSize; }     Public voidSetPageSize (intpageSize) {         This. pageSize =pageSize; }     Public LongGetalldatacount () {returnAlldatacount; }     Public voidSetalldatacount (intAlldatacount) {         This. Alldatacount =Alldatacount; }     Public LongGetnextpage () {returnNextPage; }     Public voidSetnextpage (intnextPage) {         This. NextPage =NextPage; }     Public intGetCode () {returnCode; }     Public voidSetcode (intcode) {         This. Code =Code; }     PublicString getmsg () {returnmsg; }     Public voidsetmsg (String msg) { This. msg =msg; }     Public intgettotal () {returnTotal ; }     Public voidSettotal (intTotal ) {         This. Total =Total ; }     PublicT getcontent () {returncontent; }     Public voidsetcontent (T content) { This. Content =content; }}

Android parsing JSON strings with Gson

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.