Peter Marozzi Peter Marozzi - 6 months ago 33
JSON Question

org.json.JSONException: No value for {...}

I'm not sure why I'm getting an exception, but no matter what I have in my JSON file, Android doesn't seem to be able to parse it.

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


Here's the JSON file, pretty simple stuff:

{"name":"Test 1"}


And the relevant 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);
}
}


And finally the error code:

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)


Granted, I don't have much experience with JSON, it checks as valid, so I'm assuming it's something in my Java code.

Answer

Please check this link: Decoding JSON in Java

The point is that you have to parse JSON first, then access the fields. What you do is that you create an empty JSONObject and then try to access field with name {"name":"Test 1"}

Something like this should do:

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