Showing Json Response In Textview
I have a Json response : { 'action':'true', '0':{ '_id':'58ca7f56e13823497175ee47' } } and I want to show the _id value in a TextView. I tried this : Strin
Solution 1:
This will be the correct way
JSONObjectobject = newJSONObject(response);
JSONObject object1 = object.get("0");
String message = object1.get("_id");
txtstorename.setText(message);
Solution 2:
You are getting JsonObject into JsonArray. thats why there is problem.
replace this line:
JSONArrayJarray = object.getJSONArray("0");
with This
JsonObject jsonObject = object.getJSONObject("0");
Solution 3:
StringRequest stringRequest = newStringRequest(Request.Method.POST, reg_url, newResponse.Listener<String>() {
@OverridepublicvoidonResponse(String response) {
try {
JSONObjectobject = newJSONObject(response);
JSONObject jsonObj= object.getJSONObject("0");
String message = jsonObj.getString("_id");
txtstorename.setText(message);
Solution 4:
When receiving JSON Object as response use JsonObjectRequest instead of StringRequest
JsonObjectRequest request= newJsonObjectRequest(Request.Method.POST,url, null,newResponse.Listener<JSONObject>(){@OverridepublicvoidonResponse(JSONObject response) {
try {
JSONObject jsonObj= response.getJSONObject("0");
String message = jsonObj.getString("_id");
txtstorename.setText(message);},null);
Solution 5:
You have to use getJSONObject("0") instead of getJSONArray because 0 is not an array.
An array would be indicated by [ /* stuff here */ ], which you do not have.
You have a Json object containing action and 0, action is a String, and 0 is an object.
In the 0 object you then have an _id field which you are trying to access.
So in your case something like the following:
// get the objectJSONObject object0 = object.getJSONObject("0");
String message = object0.getString("_id");
Post a Comment for "Showing Json Response In Textview"