java - Appending a list results in empty JSON array? -


i've been playing around org.json library. trying append list of enum types json object, resulting json array empty. here mean:

attribeffects supposed hold "health" , "ammo" respectively.

{"attr": [     {         "amount": [1000],         "mult": [0],         "attribid": [0],         "time": [-1],         "attribdesc": [null],         "attribname": ["health buff (1000)(debug)"],         "attribeffects": [{}]     },     {         "amount": [5],         "mult": [0],         "attribid": [1],         "time": [5],         "attribdesc": [null],         "attribname": ["devgun_ammo"],         "attribeffects": [{}]     } ]} 

here write it:

for(attribute : (list<attribute>) l) {                 jsonobject j = new jsonobject();                 j.append("attribname", a.getattribname());                 j.append("attribid", a.getattribid());                 j.append("attribdesc", a.getattribdesc());                  j.put("attribeffects", a.geteffects());                  for(object o : a.geteffects())                      system.out.println(o.tostring());  //              j.append("attribeffects", a.getaffects()); //todo: debug me                 j.append("amount", a.getamount());                 j.append("mult", a.getmult());                 j.append("time", a.gettime());                  root.append("attr", j); } 

the output of system.out.println verification:

health ammo 

any ideas?

edit: realize having array single value kind of pointless, want expandable in case of having single thing multiple effects

since json.org library old , unsupported, it'd wise switch away it. however, quick workaround since isn't serious project, did trick now:

for(attribute : (list<attribute>) l) {                 jsonobject j = new jsonobject();                 j.append("attribname", a.getattribname());                 j.append("attribid", a.getattribid());                 j.append("attribdesc", a.getattribdesc());                  jsonarray jar = new jsonarray();                  for(object o : a.getaffects())                      jar.put(o.tostring()); //todo optimize , time me                  j.put("attribeffects", jar);                  j.append("amount", a.getamount());                 j.append("mult", a.getmult());                 j.append("time", a.gettime());                  root.append("attr", j);             } 

(notice: assembling jsonarray, filling , appending instead of using jsonobject's built-in function collections)


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -