0

I am trying to extract and process some JSON data, but it is erroring when I try. This is my code:

protected void onPostExecute(String s) {
    String err=null;
    try {
        JSONObject root = new JSONObject(s);
        JSONObject user_data = root.getJSONObject("user_data");
        LASTNAME = user_data.getString("lastname");
        PASSWORD = user_data.getString("password");
        EMAIL = user_data.getString("email");
    } catch (JSONException e) {
        e.printStackTrace();
        err = "Exception: "+e.getMessage();
    }
    Intent i = new Intent(ctx, MainActivity.class);
    i.putExtra("lastname", LASTNAME);
    i.putExtra("email", EMAIL);
    i.putExtra("password", PASSWORD);
    i.putExtra("err", err);
    startActivity(i);
}

But this is the error:

org.json.JSONException: Value [] at user_data of type org.json.JSONArray cannot be converted to JSONObject

error output

5
  • what is value of root in logcat Commented Aug 21, 2016 at 10:06
  • can u paste logcat with some log.d("rootval",root); Commented Aug 21, 2016 at 10:06
  • add your response here too? then we can help. Commented Aug 21, 2016 at 10:28
  • I have reformatted your question to make it more readable, and adjusted the title too. Further edits would be welcome, if you can clarify or expand on your question some more. Commented Aug 23, 2016 at 12:13
  • STORING UN-ENCRYPTED PASSWORDS CAN BE FATAL Commented Aug 23, 2016 at 12:34

2 Answers 2

1

I think you have problem with casting Array to Object.

[..] means it is JSONArray.

{..} means it is JSONObject.

try {
    JSONArray jObj = new JSONArray(json);
    //This is how you get value from 1 element in JSONArray
    String firstObjectValue = jObj.getString(0);

} catch (JSONException e) {
    Log.e("JSON Parser", "Error parsing data " + e.toString());
}

If you looking for value need you to iterate all JSONArray by doing some simple loop.

JSONObject jsonObject = null;
     try {
         jsonObject = new JSONObject(result);
         JSONArray jsonARRAY = jsonObject.getJSONArray("nameOfJSONArray");
         for (int i = 0; i < jsonARRAY.length(); i++) {
             JSONObject jsonOBJECT = (JSONObject) jsonARRAY.get(i);
             String yourValue = jsonOBJECT.getString("valueKey");

         }
     } catch (JSONException e) {
         e.printStackTrace();
     }
Sign up to request clarification or add additional context in comments.

Comments

-1

It seems like your "user_data" is coming as json array instead of json object that you are trying to access.

you can use root.optJSONObject("user_data") to determine & return json object if your user_data is json object or will return null.

1 Comment

How i can do it root.optJSONObject("user_data") is not working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.