Reputation: 49
I want to parse data from Json with GSon in java I have this json
{
"time": 3.251,
"about_us": {
"dc:identifier": "some string",
"dc:title": "some string",
"dc:contributor": [
"1",
"2"
],
"dc:hasversion": "some string"
},
"results": {
"ocw": [
{
"title": "some string",
"url": "some string",
"language": "some string",
"university_name": "some string",
"university_url": "some string",
"description": "",
"uri": "some string5",
"similar": "some string",
"resourceType": "OCW",
"relatedoers": [
]
}
],
"otheroer": [
]
}
}
I only need to get the "ocw"
field, I am confused how to do this.
I created corresponding java classes for the json objects and use @SerializedName annotations to specify the field name to grab for each data member.
The classes are as follows.
Results
Ocw
Upvotes: 0
Views: 107
Reputation: 4712
If you have this json stored in some jsonString
:
JsonObject jsonObject = (JsonObject) new JsonParser().parse(jsonString);
JsonElement jsonElement = jsonObject.getAsJsonObject("results").getAsJsonArray("ocw").get(0);
MyObject object = new GsonBuilder().create().fromJson(jsonElement, MyObject.class);
MyObject should reflect this part:
"title": "some string",
"url": "some string",
"language": "some string",
"university_name": "some string",
"university_url": "some string",
"description": "",
"uri": "some string5",
"similar": "some string",
"resourceType": "OCW",
"relatedoers": [
]
Upvotes: 1