0

我是 Android 平台的新手,我想通过JSONParser 从 PHP Web 服务中获取值。但我没有得到数据,而是错误:No value for data25 ,data26 and data27.

感谢您对解决此问题的任何帮助。

try{
            JSONArray jArray = new JSONArray(result);
            for(int i=0;i<jArray.length();i++)
            {
            HashMap<String, String> map = new HashMap<String, String>();

              JSONObject json_data = jArray.getJSONObject(i);

            map.put("data25", "date25:" + json_data.getString("data25"));
            map.put("data26", "data26:" + json_data.getString("data26"));
            map.put("data27", "data27:" + json_data.getString("data27"));

            mylist.add(map);

            }

JSON结果如下:

[
{
    "data1": "5794",
    "data2": "2922",
    "data3": "6799",
    "data4": "2729",
    "data5": "9860",
    "data6": "4486",
    "data7": "9903",
    "data8": "0113",
    "data9": "9936",
    "data10": "5470",
    "data11": "7423",
    "data12": "1237",
    "data13": "6564",
    "data14": "1261",
    "data15": "4730",
    "data16": "7714",
    "data17": "2410",
    "data18": "7486",
    "data19": "7046",
    "data20": "8263",
    "data21": "4305",
    "data22": "6244",
    "data23": "2495",
    "data24": " 19\/09\/2012(Wed)"
},
{
    "data1": "555",
    "data2": "039",
    "data3": "353",
    "data4": "1438",
    "data5": "8720",
    "data6": "7490",
    "data7": "1294",
    "data8": "4000",
    "data9": "6017",
    "data10": "2294",
    "data11": "4042",
    "data12": "0064",
    "data13": "8441",
    "data14": "1252",
    "data15": "9609",
    "data16": "0797",
    "data17": "1680",
    "data18": "0917",
    "data19": "7492",
    "data20": "9488",
    "data21": "3253",
    "data22": "7915",
    "data23": "1071",
    "data24": " 19\/09\/2012(Wed)",
    //--heredata25,
    data26,
    data27----------"data25": "9555",
    "data26": "3039",
    "data27": "6353"
},
{
    "data1": "0395",
    "data2": "5928",
    "data3": "8998",
    "data4": "6447",
    "data5": "1827",
    "data6": "8131",
    "data7": "7454",
    "data8": "4647",
    "data9": "1892",
    "data10": "3656",
    "data11": "1989",
    "data12": "9095",
    "data13": "6826",
    "data14": "1860",
    "data15": "9766",
    "data16": "6069",
    "data17": "6050",
    "data18": "6526",
    "data19": "8392",
    "data20": "0520",
    "data21": "7540",
    "data22": "3608",
    "data23": "7430",
    "data24": " 19\/09\/2012(Wed)"
},
4

1 回答 1

1

根据您发布的响应,它是 JSONObjects 的 JSONArray。每个 JSONObject 都包含带有诸如 data1、data2...etch 之类的键的值。但是每个 JSONObject 都不包含键 data25、data26、data27。如果您不想抛出异常,即使响应不包含每个 JSONObject 中的 data25、data26、data27 键,那么您需要修改如下代码:

        JSONArray jArray = new JSONArray(result);
        for(int i=0;i<jArray.length();i++)
        {
        HashMap<String, String> map = new HashMap<String, String>();

          // getJSONObject() will throw exception if the key is not found in the jsonObject. Instead of it use optJSONObject() to get the values from JSONObject. It returns the value if contains otherwise returns the empty jsonObject.
          // JSONObject json_data = jArray.getJSONObject(i);
          JSONObject json_data = jArray.optJSONObject(i);


        //map.put("data25", "date25:" + json_data.getString("data25"));
         map.put("data25", "date25:" + json_data.optString("data25")); // returns empty string on not finding the key in JSONObject.

        //map.put("data26", "data26:" + json_data.getString("data26"));
        //map.put("data27", "data27:" + json_data.getString("data27"));

        mylist.add(map);

        }
于 2012-09-22T09:50:05.020 回答