Create a JSON Array using Java

Posted by Ankur on Stack Overflow See other posts from Stack Overflow or by Ankur
Published on 2010-03-30T09:33:05Z Indexed on 2010/03/30 9:53 UTC
Read the original article Hit count: 762

Filed under:
|
|

Hi I want to create a JSON array.

I have tried using:

JSONArray jArray = new JSONArray();
  while(itr.hasNext()){
    int objId = itr.next();
jArray.put(objId, odao.getObjectName(objId));
  }
results = jArray.toString();

Note: odao.getObjectName(objId) retrieves a name based on the "object Id" which is called objId

However I get a very funny looking array like

[null,null,null,"SomeValue",null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,"AnotherValue",null,null,null,null,null,null,null,null,null,null,"SomethingElse","AnotherOne","LastOne"]

With only "LastOne" being displayed when I retrieve it using jQuery.

The Array SHould look like

{["3":"SomeValue"],["40":"AnotherValue"],["23":"SomethingElse"],["9":"AnotherOne"],["1":"LastOne"]}

The numbers aren't showing up at all for some reason in the array that I am getting

© Stack Overflow or respective owner

Related posts about jsonarray

Related posts about jQuery