【发布时间】: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));
【问题讨论】: