Couchbase on android nested object json

93 views
Skip to first unread message

brijesh chavda

unread,
Dec 1, 2015, 7:00:02 PM12/1/15
to Couchbase Mobile
I have following document structure.


{
    id : "user1",
    "foods":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
}


I cant seem to figure out, how to use this in android using couchbase-lite for android.

This is my Pojo structure currently , which represents this document. 


class User{
private String id;
private ArrayList<Food> foods;
}

class Food{
private String id;
private String type;
}

Is this right mapping?


Any example link on github is really appreciated.

Thanks,
Brijesh

PRASANNA KUMAR

unread,
Dec 4, 2015, 1:14:38 AM12/4/15
to Couchbase Mobile
There is no changes in your model class, According to ur JSON info there is arraylist of Hashmaps.

"foods":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]

ArrayList<HashMap<Object,Object>> foodsList = (ArrayList<HashMap<Object,Object>>) document.getProperty("foods");
User user = new User();
for(HashMap<Object,Object> foodData : foodsList){
     Food food = new Food();
     food.id = foodsData.get("id");
   user.foodsList.add(food);
}

It will be related to something like this, You have to take care about this thinks while doing parsing the document

James Nocentini

unread,
Dec 9, 2015, 5:30:45 AM12/9/15
to Couchbase Mobile
You can read this blog on using model classes and JSON on Android http://jamiltz.github.io/simple-model-class-for-couchbase-lite-android/

James
Reply all
Reply to author
Forward
0 new messages