android - Put method replacing the previous object of the jsonarray -
i trying form type of jsonarray
.
[ { "useridentity":"bbb", "admin":true/false }, { "useridentity":"bbb", "admin":true/false } . . . ]
i used code in android giving input hashmap
toadd:
jsonobject inerobj=new jsonobject(); jsonarray jsonarray=new jsonarray(); (string s : toadd.keyset()) { // log.d("userid",s); inerobj.put("useridentity", s); inerobj.put("admin", boolean.parseboolean(toadd.get(s))); log.d("inerobj.tostring",inerobj.tostring()); jsonarray.put(inerobj); log.d("jsonarray.tostring",jsonarray.tostring()); }
the hashmap toadd has:
towhid37:value=true towhid32:value=true
in inerobj
value ok.but when appending inerobj
jsonarray, adds 1st value(towhid37)
ok. bt when puts 2nd value(towhid32)
replaces 1st value ie 1st element of array becomes (towhid32).
heres logcat output:
05-20 12:20:43.968: d/inerobj.tostring(3788): {"admin":true,"useridentity":"towhid37"} 05-20 12:20:43.988: d/jsonarray.tostring(3788): [{"admin":true,"useridentity":"towhid37"}] 05-20 12:20:43.988: d/inerobj.tostring(3788): {"admin":true,"useridentity":"towhid32"} 05-20 12:20:44.038: d/jsonarray.tostring(3788): [{"admin":true,"useridentity":"towhid32"},{"admin":true,"useridentity":"towhid32"}]
what problem here?
add
inerobj=new jsonobject();
inside loop.
from understand, stores reference jsonobject instead of values, why values seems overwritten. re-initialize variable add new values.
Comments
Post a Comment