1

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

DJ-DOO
  • 4,545
  • 15
  • 58
  • 98

2 Answers2

4

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

alrama
  • 618
  • 11
  • 17
  • thanks for your reply. So as I have done in the JsonAsset class, I then do the same in the Category class or just my getters and setters? Do I then write the Category[] into the parcel in the JsonAssets class? I guess I should be implementing parcelable also? – DJ-DOO Dec 04 '13 at 16:52
  • I've edited my question to include your suggestion, I'm just looking to get the 'Id' from the Category class. I'm just not sure how to write it to the parcel – DJ-DOO Dec 05 '13 at 07:42
  • You can extend your classes JsonAsset and Category with getters and setters and so on as you wish but this isn't required from Gson parser that use defined fields. Declare fields "CategoryID,"ParentCategoryID","Id" in Category class as int or long – alrama Dec 05 '13 at 09:35
  • thank you, I'm still a little confused though. I have added the fields to the Category class (I've included that in my edit above), it's the Category array that I'm confused about, adding to it and how to write it to parcel. Thanks again for your help so far... – DJ-DOO Dec 05 '13 at 11:37
  • Can anyone help please...I've tried multiple approaches here and I can't get it to work – DJ-DOO Dec 05 '13 at 15:16
  • Nobody can help you if you do not show the code of Parcel class. I think that alrama answer is right. You should accept it and for the Parcel part formulate a new question. – giampaolo Dec 06 '13 at 21:02
  • ok, I'll accept it. The problem is if I do a getter and setter for the array, it returns null, so I don't know if it is correct? – DJ-DOO Dec 09 '13 at 12:19
1

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.

DJ-DOO
  • 4,545
  • 15
  • 58
  • 98