gpt4 book ai didi

android - 解析一个json文件

转载 作者:行者123 更新时间:2023-11-29 15:27:10 24 4
gpt4 key购买 nike

我正在尝试将 json 解析到我的 ListView 中。我不知道有什么问题?但是我有一个异常告诉我名称必须是字符串并且在一个对象中必须是唯一的。

这是我的代码:

try {

jsonString = "{[{\"url\":\"www.200Chess.com\",\"price\":\"Free\",\"name\":\"200 Chess Opening\"},{\"url\":\"www.midgame.com\",\"price\":\"10$\",\"name\":\"Mid Game TacHcs\"},{\"url\":\"www.303checkmate.com\",\"price\":\"20$\",\"name\":\"303 Checkmate puzzles\"},{\"url\":\"www.303winning.com\",\"price\":\"30$\",\"name\":\"303 Winning Strategies\"},{\"url\":\"www.bibby.com\",\"price\":\"10$\",\"name\":\"Bobby Fusher Teaches Chess\"}]}";

json= new JSONObject(jsonString);

JSONArray jsonBook = json.getJSONArray("");

for (int i=0; i<jsonBook.length();i++){

Book book =new Book();

JSONObject jsonAttributes = jsonBook.getJSONObject(i).getJSONObject("");

Log.i("ListBooks", jsonAttributes.toString());

book.name = jsonAttributes.getString(KEY_NAME);

Log.i("ListBooks", book.name);

book.price = jsonAttributes.getString(KEY_PRICE);

book.url = jsonAttributes.getString(KEY_URL);

books.add(book);

}

} catch (JSONException e) {

e.printStackTrace();

}

我遇到了这个异常:

06-05 21:59:42.283: W/System.err(541): org.json.JSONException: Names must be strings, but [{"url":"www.200Chess.com","name":"200 Chess Opening","price":"Free"},{"url":"www.midgame.com","name":"Mid Game TacHcs","price":"10$"},{"url":"www.303checkmate.com","name":"303 Checkmate puzzles","price":"20$"},{"url":"www.303winning.com","name":"303 Winning Strategies","price":"30$"},{"url":"www.bibby.com","name":"Bobby Fusher Teaches Chess","price":"10$"}] is of type org.json.JSONArray at character 361 of {[{"url":"www.200Chess.com","price":"Free","name":"200 Chess Opening"},{"url":"www.midgame.com","price":"10$","name":"Mid Game TacHcs"},{"url":"www.303checkmate.com","price":"20$","name":"303 Checkmate puzzles"},{"url":"www.303winning.com","price":"30$","name":"303 Winning Strategies"},{"url":"www.bibby.com","price":"10$","name":"Bobby Fusher Teaches Chess"}]}
06-05 21:59:42.283: W/System.err(541): at org.json.JSONTokener.syntaxError(JSONTokener.java:446)
06-05 21:59:42.293: W/System.err(541): at org.json.JSONTokener.readObject(JSONTokener.java:363)
06-05 21:59:42.293: W/System.err(541): at org.json.JSONTokener.nextValue(JSONTokener.java:96)
06-05 21:59:42.293: W/System.err(541): at org.json.JSONObject.<init>(JSONObject.java:154)
06-05 21:59:42.293: W/System.err(541): at org.json.JSONObject.<init>(JSONObject.java:171)
06-05 21:59:42.293: W/System.err(541): at tn.haythem.learnchess.ListBooks.getBooks(ListBooks.java:157)
06-05 21:59:42.293: W/System.err(541): at tn.haythem.learnchess.ListBooks.onCreate(ListBooks.java:52)
06-05 21:59:42.293: W/System.err(541): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-05 21:59:42.293: W/System.err(541): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-05 21:59:42.303: W/System.err(541): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-05 21:59:42.303: W/System.err(541): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-05 21:59:42.303: W/System.err(541): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-05 21:59:42.313: W/System.err(541): at android.os.Handler.dispatchMessage(Handler.java:99)
06-05 21:59:42.313: W/System.err(541): at android.os.Looper.loop(Looper.java:123)
06-05 21:59:42.313: W/System.err(541): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-05 21:59:42.313: W/System.err(541): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 21:59:42.313: W/System.err(541): at java.lang.reflect.Method.invoke(Method.java:507)
06-05 21:59:42.313: W/System.err(541): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-05 21:59:42.313: W/System.err(541): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-05 21:59:42.323: W/System.err(541): at dalvik.system.NativeStart.main(Native Method)

任何人都可以告诉我我的代码有什么问题吗?

最佳答案

JSON 对象中的数组没有名称,这是无效的,例如它应该像这样开始:

"{\"thearrayname\":[{\"url\":\"www.200Chess.com\",\" ...

或删除外部 {} 并将其直接解析为 JSONArray 例如:

[{\"url\":\"www.200Chess.com\",\" ... 

关于android - 解析一个json文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10904958/

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