java - Loop Json array with gson -
im trying parse jsonobject , can't seem it, here 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 foreach not applicable type missing. i've tried different ways can't seem 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", } ] }
now can see data, can see payments
is in fact array (values uses []
).
that means need call rootobj.getasjsonarray("payments")
returns jsonarray
, , iterable<jsonelement>
, means loop should for(jsonelement pa : paymentsobject)
.
remember, each value of array can type of json element (object, array, string, number, ...).
you know jsonobject
, can call getasjsonobject()
on them.
json = (json data) jsonparser parser = new jsonparser(); jsonobject rootobj = parser.parse(json).getasjsonobject(); jsonarray paymentsarray = rootobj.getasjsonarray("payments"); (jsonelement pa : paymentsarray) { jsonobject paymentobj = pa.getasjsonobject(); string quoteid = paymentobj.get("quoteid").getasstring(); string dateentered = paymentobj.get("date_entered").getasstring(); bigdecimal amount = paymentobj.get("amount").getasbigdecimal(); }
Comments
Post a Comment