org.json.JSONException:{...}没有值

时间:2016-05-18 15:07:15

标签: java android json exception-handling

我不确定为什么我会收到异常,但无论我在JSON文件中有什么,Android似乎都无法解析它。

org.json.JSONException: No value for {"name":"Test 1"}

这是JSON文件,非常简单:

{"name":"Test 1"}

相关的Java:

    public WritingTest readTestFromFile(Context context) {
    BufferedReader bufferedReader;
    StringBuilder stringBuilder = new StringBuilder();
    String json;
    JSONObject object = new JSONObject();

    AssetManager assetManager = context.getAssets();

    try {
        bufferedReader = new BufferedReader(new InputStreamReader(assetManager.open("WritingTest1.json")));
        while ((json = bufferedReader.readLine()) != null) {
            stringBuilder.append(json);
        }
        bufferedReader.close();
        Log.v("Stringbuilder Output", stringBuilder.toString());
        String jsonString = stringBuilder.toString();
        object.getJSONObject(jsonString);

    } catch (IOException e) {
        e.printStackTrace();
        Log.e("IOException", "Something went wrong with opening the file.");
    } catch (JSONException e) {
        e.printStackTrace();
        Log.e("JSONException", "Something went wrong with JSON reader");
    } finally {
        return new WritingTest(object);
    }
}

最后是错误代码:

V/Stringbuilder Output: {"name":"Test 1"}
W/System.err: org.json.JSONException: No value for {"name":"Test 1"}
W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
W/System.err:     at org.json.JSONObject.getJSONObject(JSONObject.java:609)
W/System.err:     at com.holospring.prodigept.WritingSelectorFragment.readTestFromFile(WritingSelectorFragment.java:80)
W/System.err:     at com.holospring.prodigept.WritingSelectorFragment.onActivityCreated(WritingSelectorFragment.java:44)
W/System.err:     at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1983)
W/System.err:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1092)
W/System.err:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
W/System.err:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1234)
W/System.err:     at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2046)
W/System.err:     at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:174)
W/System.err:     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:597)
W/System.err:     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
W/System.err:     at android.app.Activity.performStart(Activity.java:6253)
W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
W/System.err:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err:     at android.os.Looper.loop(Looper.java:148)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

当然,我对JSON没有多少经验,它检查有效,所以我假设它是我的Java代码中的东西。

2 个答案:

答案 0 :(得分:2)

请检查此链接: Decoding JSON in Java

关键是你必须首先解析JSON,然后访问字段。您所做的是创建一个空的JSONObject,然后尝试访问名为{"name":"Test 1"}

的字段

这样的事情应该做:

JSONParser parser = new JSONParser();
JSONObject object = (JSONObject)parser.parse(jsonString);
String name = object.getString("name");

答案 1 :(得分:0)

你可以在JsonObject中使用optString()而不是getString()。您可以在下面看到详细的答案:

org.json.JSONException: No value for