在TextView中显示json响应 [英] Showing json response in TextView
问题描述
我有一个Json回复:
I have a Json response :
{
"action":"true",
"0":{
"_id":"58ca7f56e13823497175ee47"
}
}
,我想在TextView
中显示 _id 值.
我尝试过:
StringRequest stringRequest = new StringRequest(Request.Method.POST, reg_url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject object = new JSONObject(response);
JSONArray Jarray = object.getJSONArray("0");
String message = object.getString("_id");
txtstorename.setText(message);
我可以在Android Monitor中看到Json的响应,但是我的TextView
中什么都没得到!
有什么问题吗?
I can see Json response in Android Monitor but I got nothing in my TextView
!
what is the problem?
推荐答案
您必须使用getJSONObject("0")
而不是getJSONArray
,因为0
不是数组.
数组将由[ /* stuff here */ ]
指示,而您没有.
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.
您有一个包含action
和0
的Json对象,action
是一个字符串,而0
是一个对象.
You have a Json object containing action
and 0
, action
is a String, and 0
is an object.
在0
对象中,您将有一个尝试访问的_id
字段.
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 object
JSONObject object0 = object.getJSONObject("0");
String message = object0.getString("_id");
这篇关于在TextView中显示json响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!