简体   繁体   中英

Parsing JSON nested array with GSON on Android

I have looked through the forum and have across some similar question but none of which have been of any aid.

So I am taking up from where someone else has left off and this is where the issue lies. The code I'm working on uses gson to parse the json stream and that's fine and clear, then relevant data is then added to a parcel. Like below

public class JsonAssets{

    String Address;
    String Title;
    String File;
    Category [] Categories;

public void writeToParcel(Parcel paramParcel, int paramInt) {

    paramParcel.writeString(Address);
    paramParcel.writeString(Title);
    paramParcel.writeString(AudioFile);
}


private JsonAsset(Parcel in) {

    Address = in.readString();
    Title = in.readString();
    AudioFile = in.readString();
}

public ContentValues getContentValues(){

    ContentValues contentValue = new ContentValues();
    contentValue.put("ID", this.Id);
    contentValue.put("Title", this.Title);
    contentValue.put("address", this.Address);
}

EDIT

private class Category{

   String CategoryName;
   String Description;
   String ExternalLink;
   String File;
   String FileName;
   int    CategoryID;
   int    ParentCategoryID;
   int    Id;
   int    Image;

public int getCategoryID(){

return this.Id;

}

}

The database is then update with these contentValues.

The JSON looks something like this:

"assets":[
      {
         "Address":"Crator1, The Moon",
         "Title":"The Moon",
         "AudioFile":null,
         "Categories":[
            {
               "CategoryName":"Restaurants",
               "Description":"blah blah",
               "ExternalLink":"",
               "File":"",
               "FileName":"0",
               "CategoryID":0,
               "ParentCategoryID":786,
               "Id":334,
               "Image":"",
            },

I know that JSON isn't valid, I've just edited to suit. The problem is I have Categories, a nested array and I don't know how to handle that array. All I'm looking to get is the 'Id' in the Categories array.

I can't create a seperate object for it because of how the class is passed:

JsonReader reader = new JsonReader(new InputStreamReader(is, "UTF-8"));

    reader.beginObject();
    reader.nextName();
    reader.beginArray();
    JsonObject obj = null;


    while (reader.hasNext()) {
        try{
            switch(type){
            case ASSET_UPDATE:
                obj = gson.fromJson(reader, JsonAsset.class);

                break;
            }

So if anyone could tell me how to handle this, I'd greatly appreciate it. If I'm not very clear in my question apologies, just ask and I'll clarify...thanks in advance

1) Declare your obj as class you really have (JsonAsset)

JsonAsset obj = gson.fromJson(reader, JsonAsset.class);

2) Modify class to match Json string

public class JsonAssets{

String Address;
String Title;
String AudioFile;
Category[] Categories;
}

private class Category{

String CategoryName;
...
}

fromJson will return a full initialized object with nested array

So I managed to answer my own question correctly after hours and hours of heartbreaking research.

If anyone has the same issue, just follow this link:
http://www.javacreed.com/gson-deserialiser-example/

It took care of 1: Parsing the nested array correctly and it also deserialized it for me also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM