-3

I'm trying to get a value of my json. First here is my json.

{
    "status": "true",
    "userData": {
        "user_id": "USER001",
        "username": "boby",
        "password": "c83e4046a7c5d3c4bf4c292e1e6ec681",
        "fullname": "Boby Kurniawan",
        "phone": "089688xxxxxxx",
        "profilpic": null,
        "status": null,
        "flag": 1,
        "tipe": "TP001",
        "Deskripsi": "Ini tentang gua",
        "Email": "boby@mail.com"
    }
}

Ok, for the first there will be 2 json, if the login success the json will look like above, but when it fails it will return

 { "status" : "false" } 

So I need to parse it right? so I add this line

JSONObject jsonObj = new JSONObject(result);
JSONArray status = jsonObj.getJSONArray("status");

but I get this error

unhandled exception : org.json.JSONException

how can I fix it?

Shivam Oberoi
  • 1,447
  • 1
  • 9
  • 18
YVS1102
  • 2,658
  • 5
  • 34
  • 63

5 Answers5

2

unhandled exception : org.json.JSONException

ANS : you are getting that error Because status is string not a JSON Array

You should use jsonObj.getString("status") instead of jsonObj.getJSONArray("status");

Try this

try 
{


    JSONObject jsonObj = new JSONObject(result);
    String status = jsonObj.getString("status");

    Log.i("status", "->" + status);

    JSONObject jsonObject = jsonObj.getJSONObject("userData");

    Log.i("user_id", "->" + jsonObject.getString("user_id"));
    Log.i("username", "->" + jsonObject.getString("username"));
    Log.i("password", "->" + jsonObject.getString("password"));
    Log.i("fullname", "->" + jsonObject.getString("fullname"));
    Log.i("phone", "->" + jsonObject.getString("phone"));
    Log.i("profilpic", "->" + jsonObject.getString("profilpic"));
    Log.i("status", "->" + jsonObject.getString("status"));
    Log.i("flag", "->" + jsonObject.getString("flag"));
    Log.i("tipe", "->" + jsonObject.getString("tipe"));
    Log.i("Deskripsi", "->" + jsonObject.getString("Deskripsi"));
    Log.i("Email", "->" + jsonObject.getString("Email"));

} 
catch (JSONException e) 
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}

OUTPUT

enter image description here

Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
1

unhandled exception : org.json.JSONException

=> Because you are trying to get JSON Array, where as it's not JSON array but a field only.

Try:

String status = jsonObj.getString("status");
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
1
   try {
            JSONObject jsonObj = new JSONObject(result);
            String status = jsonObj.getString("status");
            JSONObject userData = jsonObj.getJSONObject("userData");
        } catch (JSONException e) {
            e.printStackTrace();
        }
Dilip
  • 2,622
  • 1
  • 20
  • 27
0

Here "status" is not an Array, its String. Json Array should enclosed by [ ]. To get value of "status" you should use,

String status = jsonObj.getString("status");

Then you need to check status value. Remember,

Json object enclosed by { }

Json array enclosed by [ ]

Also use try-catch block while fetching data,

try{
    String status = jsonObj.getString("status");
} catch (JSONException e) {
     e.printStackTrace();
}
Exigente05
  • 2,161
  • 3
  • 22
  • 42
-1

you need to surround with a try/catch:

    try {
            JSONObject jsonObj = new JSONObject(result);
            String status = jsonObj.optString("status");
        } catch (JSONException e) {
            e.printStackTrace();
        }
fuzzKitty
  • 334
  • 3
  • 7