Im trying to parse a jsonObject and can't seem to get it, here is what i got.
json = (json data)
JsonParser parser = new JsonParser();
JsonObject rootObj = parser.parse(json).getAsJsonObject();
JsonObject paymentsObject = rootObj.getAsJsonObject("payments");
for(JsonObject pa : paymentsObject){
String dateEntered = pa.getAsJsonObject().get("date_entered").toString();
}
But i get a foreach not applicable to type what am i missing. I've tried different ways but can't seem to get it. thanks
Json
{
"Name":"Test 2",
"amountCollected":"1997",
"payments":[
{
"quoteid":"96a064b9-3437-d536-fe12-56a9caf5d881",
"date_entered":"2016-05-06 08:33:48",
"amount":"1962",
},
{
"quoteid":"96a064b9-3437-d536-fe12-56a9caf5d881",
"date_entered":"2016-05-06 08:33:08",
"amount":"15",
},
{
"quoteid":"96a064b9-3437-d536-fe12-56a9caf5d881",
"date_entered":"2016-05-06 03:19:08",
"amount":"20",
}
]
}
JsonObjectis notIterable, so you cannot use it in an enhancedforloop like that. Maybe if you shared an example of the Json data you're trying to parse, we can better help you. One thought though: Are you sure the value ofpaymentsis an object, and not an array? Maybe you should have calledrootObj.getAsJsonArray("payments"). The returnedJsonArrayis iterable.json_encodethat PHP class.