Android JSONObject改组了

时间:2014-10-02 18:35:31

标签: java android json

我有以下字符串到json代码:

json = new JSONObject(
                        "{\"Questions\":{"
                        + "\"1\":[\"ELSYS\",\"ELSYS2\",\"ELSYS3\",\"ELSYS4\"],"
                        + "\"2\":[\"1024\",\"2048\",\"512\",\"64\"],"
                        + "\"3\":[\"RANDOM1\",\"RANDOM2\",\"RANDOM3\",\"RANDOM4\"],"
                        + "\"4\":[\"A DOG1\",\"A DOG2\",\"A DOG3\",\"A DOG4\"],"
                        + "\"5\":[\"SOME WORD1\",\"SOME WORD2\",\"SOME WORD3\",\"SOME WORD4\"]}}");

初始化后,我将JSON打印到LogCat:

Log.i(TAG, json.toString());

我的问题是json被网格化了。这是输出:

{"Questions":{"3":["RANDOM1","RANDOM2","RANDOM3","RANDOM4"],"2":["1024","2048","512","64"],"1":["ELSYS","ELSYS2","ELSYS3","ELSYS4"],"5":["SOME WORD1","SOME WORD2","SOME WORD3","SOME WORD4"],"4":["A DOG1","A DOG2","A DOG3","A DOG4"]}}

这里更具可读性的是来自json的解析信息:

key => 3 || value => RANDOM1,RANDOM2,RANDOM3,RANDOM4
key => 2 || value => 1024,2048,512,64
key => 1 || value => ELSYS,ELSYS2,ELSYS3,ELSYS4
key => 5 || value => SOME WORD1,SOME WORD2,SOME WORD3,SOME WORD4
key => 4 || value => A DOG1,A DOG2,A DOG3,A DOG4

正如您所看到的,字符串的顺序不正确。我在普通的Java程序中尝试了相同的代码,它运行正常。我在android中做这个问题就在那里。你知道可能是什么问题吗? (我试过清理项目)

更新

尝试" ["而不是" {"。抛出以下异常:

10-02 18:49:09.985: W/System.err(2263): org.json.JSONException: Unterminated array at character 18 of {"Questions":["1":["ELSYS","ELSYS2","ELSYS3","ELSYS4"],"2":["1024","2048","512","64"],"3":["RANDOM1","RANDOM2","RANDOM3","RANDOM4"],"4":["A DOG1","A DOG2","A DOG3","A DOG4"],"5":["SOME WORD1","SOME WORD2","SOME WORD3","SOME WORD4"]]}
10-02 18:49:09.985: W/System.err(2263):     at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
10-02 18:49:09.985: W/System.err(2263):     at org.json.JSONTokener.readArray(JSONTokener.java:440)
10-02 18:49:09.985: W/System.err(2263):     at org.json.JSONTokener.nextValue(JSONTokener.java:103)
10-02 18:49:09.985: W/System.err(2263):     at org.json.JSONTokener.readObject(JSONTokener.java:385)
10-02 18:49:09.985: W/System.err(2263):     at org.json.JSONTokener.nextValue(JSONTokener.java:100)
10-02 18:49:09.985: W/System.err(2263):     at org.json.JSONObject.<init>(JSONObject.java:154)
10-02 18:49:09.985: W/System.err(2263):     at org.json.JSONObject.<init>(JSONObject.java:171)
10-02 18:49:09.985: W/System.err(2263):     at com.tarasoft.shop.HTTPRequests$SendRequest.doInBackground(HTTPRequests.java:36)
10-02 18:49:09.985: W/System.err(2263):     at com.tarasoft.shop.HTTPRequests$SendRequest.doInBackground(HTTPRequests.java:1)
10-02 18:49:09.985: W/System.err(2263):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-02 18:49:09.985: W/System.err(2263):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-02 18:49:09.985: W/System.err(2263):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-02 18:49:09.985: W/System.err(2263):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-02 18:49:09.985: W/System.err(2263):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-02 18:49:09.985: W/System.err(2263):     at java.lang.Thread.run(Thread.java:856)

更新2 您应该知道这些键&#34; 1&#34;,&#34; 2&#34;也可以是字符串,所以我不会使用它们来排序json,我只使用它来查看订单是否正确。而不是这些&#34; 1&#34;,&#34; 2&#34;等等会有字符串。

2 个答案:

答案 0 :(得分:2)

来自http://www.json.org/

的JSON规范
  

对象是一组无序的名称/值对

不要担心订单,你无能为力。

答案 1 :(得分:1)

JSONArray是有序的,可以包含集合作为值,换句话说其他jsonArrays。使问题成为jsonArray,然后将其放在jsonObject中,如下所示:

JSONArray questionsArray = new JSONArray();

JSONArray one = new JSONArray();
one.put("ELSYS")
one.put("ELSYS2")
one.put("ELSYS3")
one.put("ELSYS4")
JSONObject firstObject = new JSONObject()
firstObject.put("question", "6*2")//(key, value)
firstObject.put("answers", one) 
questionsArray.put(firstObject ) 

JSONArray two = new JSONArray();
two.put("1024")
two.put("2048")
two.put("512")
two.put("64")
JSONObject secondObject = new JSONObject()
secondObject.put("question", "hello world?")
secondObject.put("answers", two) 
questionsArray.put(secondObject)

//do this for all your items then add the questions array to your jsonObject
JSONObject mainJsonObj = new JSONObject()
mainJsonObj.put("questions", questionsArray);

//then when you need the data you will go like this:
questionsArray = mainJsonObj.optJSONArray("questions")

//then you have your ordered array containing your question/answer objects, the answers of which will be ordered. To get them you will have to get by index like:
objOne = questionsArray.get(0) //etc
String question = objOne.optString("question")
JSONArray answers = objOne.optString("answers")

沿着这些方向,你可以试着让它更优雅,这只是为了展示基本的想法。或者,您将不得不查看不同的数据结构,如linkedHashMap / arrayList

修改

当您需要字符串值时,您需要使用json对象和数组的组合。要记住的重要一点是,这个JSONArray将维护它的顺序,而JSONObject将不会保持其顺序(并且你无法做很多事情)。我将编辑上面的代码以便于阅读。最初在你的问题中设想它的方式不可能维持它的顺序,所以这个解决方案可能不完全像你想要它,但它可能是最接近你不得不使用json。您可能希望将这些部分包装在一个函数中,并将问题和答案列表作为值传递,这样您就不需要重复所有代码,但现在不要担心。首先看看这个解决方案是否适合您的需求。