jQuery 如何创建 JSON 数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2544119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create a JSON Array?
提问by Ankur
Hi I want to create a JSON array.
嗨,我想创建一个 JSON 数组。
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.
注意:odao.getObjectName(objId)
根据名为 objId 的“对象 ID”检索名称。
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
.
当我使用jQuery
.
The Array SHould look like
数组应该看起来像
{["3":"SomeValue"],["40":"AnotherValue"],["23":"SomethingElse"],["9":"AnotherOne"],["1":"LastOne"]}
{["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.
由于某种原因,这些数字根本没有出现在我得到的数组中。
回答by Buhake Sindi
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:
根据TJ Crowder 的回应,我的解决方案是这样做的:
[{"3":"SomeValue"},
{"40":"AnotherValue"},
{"23":"SomethingElse"},
{"9":"AnotherOne"},
{"1":"LastOne"}
]
Refer to Jim Blackler's comment of what you're doing wrong.
请参阅 Jim Blackler 对您做错了什么的评论。
回答by Jim Blackler
The clue is in the documentation for JSONArray for method put(int index, String value)
线索在 JSONArray 的文档中,用于方法 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.
如果索引大于 JSONArray 的长度,则将根据需要添加空元素以填充它。
回答by T.J. Crowder
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.orgfor the syntax of JSON.
您为“对象应该看起来像”引用的内容是无效的 JSON。它是一个对象(由{
和分隔}
),但其中包含没有任何键的值。有关JSON 的语法,请参阅json.org。
If you want this:
如果你想要这个:
{"3":"SomeValue",
"40":"AnotherValue",
"23":"SomethingElse",
"9":"AnotherOne",
"1":"LastOne"
}
...use JSONObjectinstead, and turn your objId
s into keys when putting the entries in, e.g.:
...使用JSONObject代替,并objId
在放入条目时将您的s 转换为键,例如:
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).
...请参阅精英绅士的回答(即 JSONObjects 的 JSONArray)。