リストビューに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)
私のコードで何が間違っているのか教えてください。