Search code examples
javaandroidrealm

Get all objects on ArrayList


can some one explain me how to get all categories value from

"categories":[{"1":1,"2":"orange","3":"mango","4":"guava","5":5,"6":6}]

result my like this 1 = 1, and 2 = orange,

what must i do i am stuck in here

public RealmList<CategoryRealm> categories;

or

 p.categories = new RealmList<>();

can some one explain to me what must i do in the next method i am stuck tried searching but so damn hard to learn its diferent.


Solution

  • Use GSON library.

    Create an object that matches your structure. I'm assuming you have a structure of

    {
        "categories"://the rest of the stuff here
    }
    
    
    class MyParentObject{
       @SerializeName("categories")
       ArrayList<String> myList;
    }
    

    Then use GSON to create it

     MyParentObject obj = (MyParentObject)getGson().fromJson(json, classType);
    

    and your done.

    If the base is just the categories string then your json is badly formatted and you may have to do a subString call to get starting index of "[" and go from there into json management.