Wednesday, 15 September 2010

android - JSON tag returns empty array [] while gson expects a string -


I am using the GSON library to parse my JSON tag

Some tags are string values ​​(not array) The problem is, sometimes the element is empty [] and when this happens the console returns this error

  expected The string was BEGIN ARRAY   

The following is the ideal case for my JSON

  {"Internet": "600.00", "internet_remarks": "Fiber 1 Gbps ",}   

but sometimes it gets

  {" Internet ":" 600.00 "," internet_remarks ": [],}   

My parsing code is as follows:

  GSF Blider Sonbuilder = new GsonBuilder (); GSON GSN = GSOnBiller.Create (); Main Controller Main Container = GSN From Jason (obj, MainContainer.class);   

is in my class, while the following is that varailbas has been defined

  private string Internet; Private string internet_mark;   

My question is what should I change so that the empty array in the variable is adjusted []

If you do not control the source of the (poorly designed) JSON and you can not fix it, then you have to write a custom deserillizer that will be sent to your MainContainer See the object

here and / or in the information

If the only time in that area is an array type, when it is a So Ali is an array, then the easiest way I can think of is to simply observe returned JSON and if it is an array, remove it. Then, deselect your main container . GSO silently ignores any missing elements in JSON and internet_remarks will be null in your code MainContainer .

  Class MyDeserialier Applicable JsonDeserializer & lt; MainContainer & gt; {@ Override Public Men Container Desariel (Jason Element J, Type Type, JasonDesignalization Contact JDC) Jason Pars throws expansion (JasonObject ozz = J.A.J.S.S..S..S.Jason; Object; If (obj.get ("internet_remarks"). IsJsonArray ()) {obj.remove ("internet_remarks"); } New GSN () Return to JSON (OBJ, MAIN CONTAINER Class):}}   

If this is not really the case and that array can not be empty, then you have to deal with logic and make it one To convert string to , if you really want

No comments:

Post a Comment