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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

7 Comments

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?
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
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
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...
Can anyone help please...I've tried multiple approaches here and I can't get it to work
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.