【问题标题】:Error: No value for "ColumnName". Get JSON value in Android错误:“ColumnName”没有值。在 Android 中获取 JSON 值
【发布时间】:2017-12-05 13:36:28
【问题描述】:

我想将 f_index 值从 JSON 获取到我的 Android 应用程序中。我收到一个错误“f_index 没有值。

W/System.err: org.json.JSONException: No value for f_index

如果我删除以下行,那么我可以正确获取 lat、long 和 is_local 值并进一步使用它们。

ill_id_data.add(ill_jo.getInt("f_index"));

我无法理解这里出了什么问题?

在我传递给解析方法的应用程序中收到的 JSON。

{
"server_response": [{
    "f_index": "3",
    "lat": "21.9159",
    "long": "78.1021",
    "is_local": "0"
}, {
    "f_index": "6",
    "lat": "25.0751",
    "long": "54.9476",
    "is_local": "0"
}, {
    "f_index": "7",
    "lat": "14.6773",
    "long": "75.542",
    "is_local": "1"
}, {
    "f_index": "8",
    "lat": "21.9159",
    "long": "78.1021",
    "is_local": "0"
}, {
    "f_index": "9",
    "lat": "25.8079",
    "long": "86.0449",
    "is_local": "1"
}]
}

在我的应用程序中解析 JSON 的代码。

ArrayList<Double> ill_lat_data = new ArrayList<Double>(3);
ArrayList<Double> ill_long_data = new ArrayList<Double>(3);
ArrayList<Interger> ill_id_data = new ArrayList<Integer>(3);
ArrayList<Integer> ill_is_local_data = new ArrayList<Integer>(3);

public void parseIndexLatLngJSON() {

    int count_ill = 0;

    try {
        ill_json_object = new JSONObject(json_index_latlng);
        ill_json_array = gm_json_object.getJSONArray("server_response");

        while (count_ill < ill_json_array.length()) {
            JSONObject ill_jo = ill_json_array.getJSONObject(count_ill);

            ill_id_data.add(ill_jo.getInt("f_index"));
            Log.d("tag_last_check", String.valueOf(ill_jo.getInt("f_index")));
            ill_lat_data.add(ill_jo.getDouble("lat"));
            ill_is_local_data.add(ill_jo.getInt("is_local"));
            ill_long_data.add(ill_jo.getDouble("long"));

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

这是我的 php 文件。

$sql="SELECT id,lat,`long` FROM newsphere_one WHERE lat != 0 OR `long` != 0;";

$con=mysqli_connect($host,$hostname,$password,$db);

$result = mysqli_query($con,$sql);

$response = array();

while($row=mysqli_fetch_array($result)){
array_push($response, array("f_index" => $row[0],"lat" => $row[1],"long" => $row[2],"is_local" => $row[3]));
}

echo json_encode(array("server_response"=>$response));

【问题讨论】:

    标签: php android json


    【解决方案1】:

    首先,

    ill_id_data.add(ill_jo.getInt("f_index"));  
    

    尝试获取一个整数,而在您的 json 中它被定义为一个字符串。 尝试获取字符串,然后将其转换为 int。尝试使用:

    Integer.parseInt()
    

    【讨论】:

    • 我试过这个 - ill_id_data.add(Integer.valueOf(ill_jo.getString("f_index")));但仍然是同样的错误。如果你说的是这种情况,我怎么会得到'is_local'。
    • 您的 json 结构很可能有错误。尝试使用 optString("f_index")。如果没有找到“f_index”,它将返回一个空字符串。如果是这种情况,使用 optString 应该不会导致错误
    • 是的,我也尝试过 optString 和 OptInt。它不会导致错误,但日志会打印 0,这意味着我没有得到该值。在打印 JSON 字符串时,我清楚地看到有一个值。
    • 这很奇怪.. 你能发布完整的日志吗? “server_response”数组。
    • 我随问题发布的 JSON 正是应用程序中的响应。
    【解决方案2】:

    再次仔细观察下面两行

    ill_json_object = new JSONObject(json_index_latlng);
    ill_json_array = gm_json_object.getJSONArray("server_response");
    

    在第 1 行中,您已将 json_index_latlng 分配给对象 ill_json_object

    但在第 2 行,您正在访问来自 gm_json_object 的服务器响应。

    再次检查您的代码, 祝你好运

    【讨论】:

      【解决方案3】:

      在php中

      更改下面的行
      array_push($response, array("f_index" => $row[0],"lat" => $row[1],"long" => $row[2],"is_local" => $row[3]));
      

      array_push($response, array("f_index" => intval($row[0]),"lat" => $row[1],"long" => $row[2],"is_local" => $row[3]));
      

      使用intval($row[0]) 转换为整数

      使用floatval($row[0]) 转换为浮点数

      【讨论】:

      • 我将它转换为 intval 但它不起作用。我得到没有“”的JSON,即像这样 {"server_response": [{"f_index": 3,"lat": "21.9159",.... 后来在获取值时得到了同样的错误。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 2013-12-10
      • 2013-03-29
      相关资源
      最近更新 更多