我想解析一個json到我的列表視圖中。我不是什麼問題?但我得到一個異常告訴我名稱必須是字符串,並且在一個對象內必須是唯一的。解析一個json文件
這裏是我的代碼:
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)
任何機構可以告訴我什麼是錯在我的代碼?