Reputation: 3402
Iam using volley in my app for network requests.I am trying to use gson library for json parsing and create a list view with that.Json looks like this.When it try to iterate I get only null objects.What is wrong with this and what is missing
{
"header": {
"count": 7,
"expiryTime": 1000
},
"channel": [
{
"code": "/par/feed_section",
"name": "News",
"type": "news",
"title": "News",
"offline": "1",
"sub": [
{
"parentCode": "/par/feed_section",
"name": "Just In",
"type": "news",
"code": "/par/feed_sub_section",
"title": "Just In",
"offline": "1"
},
{
"parentCode": "/par/feed_section",
"name": "Kerala",
"type": "news",
"code": "/par/feed_sub_section_0",
"title": "Kerala",
"offline": "1"
},
{
"parentCode": "/par/feed_section",
"name": "Nation",
"type": "news",
"code": "/par/feed_sub_section_1",
"title": "Nation",
"offline": "1"
},
{
"parentCode": "/par/feed_section",
"name": "World",
"type": "news",
"code": "/par/feed_sub_section_2",
"title": "World",
"offline": "1"
},
{
"parentCode": "/par/feed_section",
"name": "Politics",
"type": "news",
"code": "/par/feed_sub_section_3",
"title": "Politics",
"offline": "1"
},
{
"parentCode": "/par/feed_section",
"name": "Diaspora",
"type": "news",
"code": "/par/feed_sub_section_5",
"title": "Diaspora",
"offline": "1"
},
{
"parentCode": "/par/feed_section",
"name": "Science & Tech",
"type": "news",
"code": "/par/feed_sub_section_6",
"title": "Science & Tech",
"offline": "1"
},
{
"parentCode": "/par/feed_section",
"name": "Opinion Pieces",
"type": "news",
"code": "/par/feed_sub_section_7",
"title": "Opinion Pieces",
"offline": "1"
}
]
},
{
"code": "/par/feed_section_5",
"name": "Sports",
"type": "news",
"title": "Sports",
"offline": "1",
"sub": [
{
"parentCode": "/par/feed_section_5",
"name": "Cricket",
"type": "news",
"code": "/par/feed_sub_section",
"title": "Cricket",
"offline": "1"
},
{
"parentCode": "/par/feed_section_5",
"name": "Football",
"type": "news",
"code": "/par/feed_sub_section_1",
"title": "Football",
"offline": "1"
},
{
"parentCode": "/par/feed_section_5",
"name": "Tennis",
"type": "news",
"code": "/par/feed_sub_section_2",
"title": "Tennis",
"offline": "1"
},
{
"parentCode": "/par/feed_section_5",
"name": "Motor Sports",
"type": "news",
"code": "/par/feed_sub_section_3",
"title": "Motor Sports",
"offline": "1"
},
{
"parentCode": "/par/feed_section_5",
"name": "Other Sports",
"type": "news",
"code": "/par/feed_sub_section_0",
"title": "Other Sports",
"offline": "1"
}
]
},
{
"code": "/par/feed_section_1",
"name": "Business",
"type": "news",
"title": "Business",
"offline": "1",
"sub": [
{
"parentCode": "/par/feed_section_1",
"name": "News",
"type": "news",
"code": "/par/feed_sub_section",
"title": "News",
"offline": "1"
},
{
"parentCode": "/par/feed_section_1",
"name": "Markets",
"type": "news",
"code": "/par/feed_sub_section_0",
"title": "Markets",
"offline": "1"
},
{
"parentCode": "/par/feed_section_1",
"name": "Companies",
"type": "news",
"code": "/par/feed_sub_section_1",
"title": "Companies",
"offline": "1"
},
{
"parentCode": "/par/feed_section_1",
"name": "Autos",
"type": "news",
"code": "/par/feed_sub_section_2",
"title": "Autos",
"offline": "1"
}
]
}
] }
and my classes is like
public class Channel {
String code;
String name;
String type;
String title;
String offline;
List<Sub> sub; }
public class Sub {
String name;
String type;
String code;
String title;
String offline;
String parentCode; }
I use volley and try to parse result like this
GsonRequest<Channel> myReq = new GsonRequest<Channel>(Method.GET,
"http://JSONURL/",
Channel.class,
createMyReqSuccessListener(),
createMyReqErrorListener());
I am stuck at this point.How to iterate through the result.I am new to GSON.So please help me
Upvotes: 0
Views: 155
Reputation: 16428
Your domain model is slightly off, you need to have a root object and set channel to be a list, since its an array in your json.
Try this:
import com.google.gson.Gson;
import java.util.List;
public class TestMe {
public static void main(String[] args) {
String jsonString = "paste your json here, omitted from example as its noise...";
RootObject object = new Gson().fromJson(jsonString, RootObject.class);
for (Channel c : object.channel){
System.out.println(String.format("Channel %s has %d subs", c.name, c.sub.size()));
}
}
}
class RootObject {
Header header;
List<Channel> channel;
}
class Header {
int count;
int expirationTime;
}
class Channel {
String code;
String name;
String type;
String title;
String offline;
List<Sub> sub;
}
class Sub {
String parentCode;
String name;
String type;
String code;
String title;
String offline;
}
Example output of the above:
Channel News has 8 subs
Channel Sports has 5 subs
Channel Business has 4 subs
Hope this helps!
Upvotes: 1