在TextView中显示json响应 [英] Showing json response in TextView

查看:273
本文介绍了在TextView中显示json响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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.

您有一个包含action0的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆