Reputation: 109
I am parsing json in Android.My json parse sucessfully .but i don't know how to handle this.
{
"result": {
"response": "OK",
"message": "Authentication was successful.",
"credencial": {
"credencial": {
"name": "hello world",
"username": "a",
"email": "[email protected]",
"id": "58"
}
}
}
}
This is my json.after parsing how i can access "message" and other info.
protected void onPostExecute(JSONObject jsonObject) {
if (jsonObject != null) {
String jsondata, ddd;
try {
//JSONObject jsonObj = new JSONObject("result");
jsondata = jsonObject.getString("result");
Toast.makeText(getApplicationContext(), jsondata,
Toast.LENGTH_LONG).show();
pDialog.dismiss();
Intent i = new Intent(LoginActivity.this, MainActivity.class);
startActivity(i);
finish();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getApplicationContext(), "exception",
Toast.LENGTH_SHORT).show();
pDialog.dismiss();
}
}
//}
}
here Toast Sucessfully shows me total Json but i want to access next information like "message","email" and "id".Please ignore mistakes in question syntax
Upvotes: 0
Views: 45
Reputation: 11038
Instead of calling getString
call getJSONObject
.
Check out the docs here: https://developer.android.com/reference/org/json/JSONObject.html#getJSONObject(java.lang.String)
Upvotes: 1
Reputation: 3805
protected void onPostExecute(JSONObject jsonObject) {
if (jsonObject != null) {
String jsondata, ddd;
try {
JSONObject jsonObj = jsonObject.getJsonbject("result");
JSONObject credencialObj1 = jsonObj.getJsonObject("credencial");
JSONObject credencialObj2 = credencialObj1.getJsonObject("credencial");
String name = credencialObj2.getString("name");
// so on
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getApplicationContext(), "exception",
Toast.LENGTH_SHORT).show();
pDialog.dismiss();
}
}
}
Upvotes: 0
Reputation: 13785
Convert jsondata to again jsonobject and then fetch message
JSONObject jsonObj = new JSONObject(jsondata);
String message = jsonObj.getString("message");
Upvotes: 0