How do I create a JSON Array?

后端 未结 3 1389
[愿得一人]
[愿得一人] 2021-01-13 06:31

Hi I want to create a JSON array.

I have tried using:

JSONArray jArray = new JSONArray();
  while(itr.hasNext()){
    int objId = itr.next();
jArray.         


        
3条回答
  •  轮回少年
    2021-01-13 06:57

    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.

提交回复
热议问题