gpt4 book ai didi

java - Android JSONObject 改组

转载 作者:行者123 更新时间:2023-12-01 22:40:52 28 4
gpt4 key购买 nike

我有以下字符串到 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你应该知道这些键“1”、“2”等也可以是字符串,所以我不使用它们对 json 进行排序,我只使用它们来查看顺序是否正确。将会有字符串代替这些“1”、“2”等。

最佳答案

来自 JSON 规范 http://www.json.org/

An object is an unordered set of name/value pairs

不用担心顺序,您对此无能为力。

关于java - Android JSONObject 改组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26167301/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com