【问题标题】:JSONException No ValueJSONException 无值
【发布时间】:2014-05-27 05:57:09
【问题描述】:

我创建了以下价格和手机型号名称的 json 数组。此数据是从 wamp phpmyadmin 服务器中读取的,表名为“products”:

    {
    "products": [
        {
            "pid": "14",
            "name": "zxc",
            "price": "123456"
        },
        {
            "pid": "6",
            "name": "Sony    Xperia",
            "price": "35000"
        },
        {
            "pid": "8",
            "name": "Samsung Galaxy Note",
            "price": "32000"
        },
        {
            "pid": "5",
            "name": "htc",
            "price": "26326"
        },
        {
            "pid": "9",
            "name": "Nokia Lumia 800",
            "price": "18000"
        },
        {
            "pid": "2",
            "name": "iphone",
            "price": "12345"
        },
        {
            "pid": "15",
            "name": "sdjnas",
            "price": "12243"
        },
        {
            "pid": "13",
            "name": "Samsung S5222",
            "price": "6500"
        },
        {
            "pid": "11",
            "name": "Nokia C201",
            "price": "4400"
        },
        {
            "pid": "7",
            "name": "Nokia Asha 200",
            "price": "4000"
        },
        {
            "pid": "1",
            "name": "htc",
            "price": "1234"
        },
        {
            "pid": "3",
            "name": "htc",
            "price": "1234"
        },
        {
            "pid": "4",
            "name": "htc",
            "price": "1234"
        },
        {
            "pid": "10",
            "name": "aks",
            "price": "1234"
        },
        {
            "pid": "12",
            "name": "asd",
            "price": "123"
        }
    ],
    "success": 1
}

我对以下代码块有疑问:

    try 
    {
        int success = json.getInt(TAG_SUCCESS);

        if (success == 1)
        {
                products = json.getJSONArray(TAG_PRODUCTS);

                for (int i = 0; i < products.length(); i++)
                {
                    JSONObject c = products.getJSONObject(i);

                    String id = c.getString(TAG_PID);
                    String name = c.getString(TAG_NAME);
                }
        }

            else
            {
                Intent i = new Intent(getApplicationContext(), NewProductActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
    }

    catch (JSONException e)
    {
        e.printStackTrace();
    }

其中标签是简单的字符串值:

private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PID = "pid";
private static final String TAG_NAME = "name";

每当我运行此代码时,它都会显示在 logcat 中,它会显示 JSONException No Value 成功。我无法弄清楚究竟是什么导致了这个问题。先感谢您。

【问题讨论】:

  • 你打印了 json 吗?
  • 什么是json。是jsonobject吗?
  • @Hariharan yaa 我刚刚在上面给出的字符串打印在 logact 中
  • @GiruBhai "success" 在最后,值为 1
  • 您是否收到 json 输出作为响应?请先检查一下。你解析代码对我来说很好

标签: java android json


【解决方案1】:

检查:

JSONObject json = jParser.makeHttpRequest(URL,
                    POST, params);

从那里How to httpPost() to covert httpGet()

【讨论】:

  • 我有一个 JSONParser 类,它已经实现了这种方式。
【解决方案2】:

请检查,

你用来获取JSON的方法说getJSONFromUrl()

HttpPost 更改为HttpGet

请参考这个Link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    • 2017-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多