5

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.

3
  • You have a JSON Object of JSON arrays? Commented Mar 30, 2010 at 9:41
  • 1
    Whatever you get, it shouldn't look like that. Arrays are [foo,bar,baz] while objects are { "foo": "value", "bar": "value", "baz": "value" }. You seem to have your {} and [] confused. Commented Mar 30, 2010 at 9:41
  • 1
    Thanks everyone, I got something from each answer. In the end I realised my question wasn't expressed exactly as it should have been, but your answers solved my problem. Commented Mar 30, 2010 at 13:32

3 Answers 3

12

For your quick Solution:

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

results = jArray.toString();

Based on T. J. Crowder's response, my solution does this:

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

Refer to Jim Blackler's comment of what you're doing wrong.

Sign up to request clarification or add additional context in comments.

Comments

8

The clue is in the documentation for JSONArray for method put(int index, String value)

If the index is greater than the length of the JSONArray, then null elements will be added as necessary to pad it out.

2 Comments

I see so the index is literally the index of the Array and not some value I can assign.
@Ankur: Yes, it's an array in the numerically-index-array sense, not a map (aka dictionary aka associative array). You can use JSONObject (json.org/javadoc/org/json/JSONObject.html) for a map; the keys in JSON objects are always strings, so you'll need to make a string out of your objId, but that's easy enough.
2

What you've quoted for your "The object should look like" is invalid JSON. It's an object (delimited by { and }) but then it has values within it that don't have any keys. See json.org for the syntax of JSON.

If you want this:

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

...use JSONObject instead, and turn your objIds into keys when putting the entries in, e.g.:

JSONObject obj = new JSONObject();
while(itr.hasNext()){
    int objId = itr.next();
    obj.put(String.valueOf(objId), odao.getObjectName(objId));
}
results = obj.toString();

If you want this:

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

...see The Elite Gentleman's answer (that's a JSONArray of JSONObjects).

1 Comment

Until we know what he/she really wants, every answer here is correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.