解析json文件

时间:2012-06-05 21:05:34

标签: android 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)

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

3 个答案:

答案 0 :(得分:3)

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

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

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

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

答案 1 :(得分:2)

您必须使用下面显示的格式创建JSON。正是因为您的JSON文件结构不正确,您无法正确解析它。

格式正确:

{
    "an": [
        {
            "url": "www.200Chess.com",
            "price": "Free",
            "name": "200ChessOpening"
        }
    ]
}

答案 2 :(得分:0)

尝试将JSON粘贴到JSONLint.com中,以获得更好的gui语法检查器