Reputation:
I am having hard time to figure it out how to parse the following JSON (Dynamic):
[{
"existingData": [
0,
0
],
"guestId": {
"__type": "Pointer",
"objectId": "EB1rr6Lqtp"
},
"listingAddressGeopoint": {
"__type": "GeoPoint",
"latitude": 36.002702,
"longitude": -78.90682099999998
},
"numberOfListingImages": 1,
"preferredGender": "\"Female\"",
"urlOfListingBeds": [
"https://xyz.image0.jpg"
],
"urlOfPrimaryImage": null,
"createdAt": "2015-09-09T14:54:36.139Z",
"updatedAt": "2015-09-15T14:46:41.988Z",
"user": {
"createdAt": "2015-09-09T14:54:34.841Z",
"updatedAt": "2015-09-09T14:54:34.841Z"
}
}]
The problem is sometime data starts previuosData
not from existingData
. How can I get the array of urlOfListingBeds
in list of Some object?
public class Image {
public List<String> urlOfListingBeds;
}
I tried to access it by following code but it is throwing an errror
for (int i = 0; i < rjson.size(); i++) {
rjson.getAsJsonObject(i);
}
where as rjson is JsonArray
Upvotes: 0
Views: 94
Reputation: 41200
Get the JsonArray from json object by calling getAsJsonArray()
. Create a iterator by calling JsonArray#iterator, and iterate over each JsonElement, and get JsonObject
by calling JsonElement#getAsJsonObject()
.
Once you have JsonObject
, you will find the urlOfListingBeds
.
Code:
JsonArray array= rjson.getAsJsonArray();
Iterator iterator = array.iterator();
List<String> urlOfListingBeds = new ArrayList<String>();
while(iterator.hasNext()){
JsonElement jsonElement = (JsonElement)iterator.next();
JsonObject jsonObject = jsonElement.getAsJsonObject();
JsonArray urlOfListingBed = jsonObject.getAsJsonArray("urlOfListingBeds");
if(urlOfListingBed!=null){
Iterator iter = urlOfListingBed.iterator();
while(iter.hasNext()){
JsonElement jsonElementChild = (JsonElement)iter.next();
if(jsonElementChild!=null)
urlOfListingBeds.add(jsonElement.getAsString());
}
}
}
Upvotes: 1