-1

here is the json

{
    "test": {
        "val": "hello"
    }
}

and here is the function I use

public static String loadJSONFromAsset(Context context) {
    String json = null;
    try {
        InputStream is = context.getAssets().open("convert.json");

        int size = is.available();

        byte[] buffer = new byte[size];

        is.read(buffer);
        JSONObject obj = new JSONObject();


        String value = obj.getJSONObject("test").optString("val");


        Log.e("test", "test: "+value);

        is.close();

        json = new String(buffer, "UTF-8");


    } catch (IOException ex) {
        ex.printStackTrace();
        Log.e("Nope", "Nope");
        return null;
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return json;

}

the error

06-28 16:12:22.210 6876-6876/com.currencyconverter W/System.err: org.json.JSONException: No value for test

its saying that there is no value in test I know there is no value in test but why it dont get the val value i even tried to make the json more simple like this

    {

        "val": "hello"
}

but same thing no value in val

1 Answers1

0

JSONArray USD_AED = (JSONArray) jsonObject.get("USD_AED");

Should be something like

JSONObject USD_AED = (Object) jsonObject.get("USD_AED");

Hakan Saglam
  • 211
  • 2
  • 7