如何创建一个JSON阵列? [英] How do I create a JSON Array?
问题描述
您好我想创建一个JSON阵列。
我已经尝试使用:
JSONArray jArray =新JSONArray();
而(itr.hasNext()){
INT OBJID = itr.next();
jArray.put(OBJID,odao.getObjectName(OBJID));
}
结果= jArray.toString();
请注意: odao.getObjectName(OBJID)
检索基于对象ID,这就是所谓的ObjID名称
不过,我得到一个非常好笑的看着像数组
<$p$p><$c$c>[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\"]由于只有LastOne当我找回它使用显示的jQuery
。
该数组应该看起来像
{[\"3\":\"SomeValue\"],[\"40\":\"AnotherValue\"],[\"23\":\"SomethingElse\"],[\"9\":\"AnotherOne\"],[\"1\":\"LastOne\"]}
块引用>数字无法显示所有由于某种原因在数组中,我得到。
解决方案有关您的快速解决方案:
JSONArray jArray =新JSONArray();
而(itr.hasNext()){
JSONObject的JSON =新的JSONObject();
INT OBJID = itr.next();
json.put(Integer.toString(OBJID),odao.getObjectName(OBJID));
jArray.put(JSON);
}结果= jArray.toString();基于 T.
J.克罗德的反应
,我的解决办法做到这一点:[{3:someValue中},
{40:AnotherValue},
{23:SomethingElse},
{9:AnotherOne},
{1:LastOne}
]请参考你在做什么错吉姆Blackler的评论。
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.
解决方案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.
这篇关于如何创建一个JSON阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!