我想从JSON获取f_index值到我的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));
答案 0 :(得分:1)
首先,
ill_id_data.add(ill_jo.getInt("f_index"));
尝试获取一个整数,而在json中它被定义为一个字符串。 尝试获取字符串然后将其转换为int。尝试使用:
Integer.parseInt()
答案 1 :(得分:0)
在php中
从
更改以下行Property binding ngForOf not used by any directive on an embedded template
到
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])
转换为float
答案 2 :(得分:0)
再次仔细观察以下两行
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 访问服务器响应。
再次检查您的代码, 祝你好运