Android obj.getJSONArray未获取JSON响应
我正试图从Android obj.getJSONArray未获取JSON响应,android,json,Android,Json,我正试图从PHP获取一个包含纬度和经度的响应,所以在我的应用程序收到这些坐标后,它应该根据地图上的坐标添加标记。据介绍,JSON响应是有效的,但应用程序似乎不想添加标记 .java代码,用于检索JSON响应: @Override protected void onPostExecute(String s) { super.onPostExecute(s); //hiding the progressbar after complet
PHP
获取一个包含纬度和经度的响应,所以在我的应用程序收到这些坐标后,它应该根据地图上的坐标添加标记。据介绍,JSON
响应是有效的,但应用程序似乎不想添加标记
.java代码,用于检索JSON
响应:
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//hiding the progressbar after completion
try {
//converting response to json object
JSONObject obj = new JSONObject(s);
//if no error in response
if (!obj.getBoolean("error")) {
Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
JSONArray locatieArray = obj.getJSONArray("locatie");
for (int i = 0; i < locatieArray.length(); i++) {
JSONObject locatie = locatieArray.getJSONObject(i);
// check latitudine and longitudine is not null and if not null then cast these values and call the addMarker() method.
if(!locatie.isNull("latitudine") && !locatie.isNull("longitudine")) {
latitudine_sql =Double.valueOf(locatie.getString("latitudine"));
longitudine_sql = Double.valueOf(locatie.getString("longitudine"));
addMarker(latitudine_sql, longitudine_sql); // this method is implemented below
}
tip_problema_sql = locatie.getString("tip_problema");
}
} else {
Toast.makeText(getApplicationContext(), "Some error occurred", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
JSON
应用程序检索到的响应:
{
"error": false,
"message": "Alerta raportata",
"locatie": {
"1": {
"latitudine": 37.4254,
"longitudine": -122.08,
"tip_problema": "Semafor nefunctional"
},
"2": {
"latitudine": 37.4259,
"longitudine": -122.088,
"tip_problema": "Semafor nefunctional"
},
"3": {
"latitudine": 37.4259,
"longitudine": -122.088,
"tip_problema": "Semafor nefunctional"
},
"4": {
"latitudine": 37.4207,
"longitudine": -122.085,
"tip_problema": "Semafor nefunctional"
}
}
}地点不是JSONArray。根据您在问题中提到的回答,这是一个JSONObject 您正在尝试访问JSONArray,但在您的响应中,JSONObject中的“locatie”不是JSONArray
{
} //This indicates JSONObject.
[
] //This indicates JSONArray.
JSONArray locatiarray=obj.getJSONArray(“locatie”)
您需要在服务器端修改php响应。您的回答应该如下所示:
{
"error": false,
"message": "Alerta raportata",
"locatie": [
{
"latitudine": 37.4254,
"longitudine": -122.08,
"tip_problema": "Semafor nefunctional"
},
{
"latitudine": 37.4259,
"longitudine": -122.088,
"tip_problema": "Semafor nefunctional"
},
{
"latitudine": 37.4259,
"longitudine": -122.088,
"tip_problema": "Semafor nefunctional"
},
{
"latitudine": 37.4207,
"longitudine": -122.085,
"tip_problema": "Semafor nefunctional"
}
]
}
若如图所示更改服务器端的响应,则无需更改应用程序端代码。它会很好地工作。类似于演示:
<?php
// Declare two dimensional associative
// array and initilize it
$arr['locatie'] [] = array (
1=>array(
"latitudine"=>1,
"longitudine"=>"Doorbell",
"tip_problema"=>199
),
2=>array(
"latitudine"=>2,
"longitudine"=>"Bottle",
"tip_problema"=>99
),
3=>array(
"latitudine"=>3,
"longitudine"=>"Washing Machine",
"tip_problema"=>7999
)
);
// Function to convert array into JSON
echo json_encode($arr);
?>
请根据您对数组值的要求进行设置,如:latitudine、longitudine、tip_problem a useforeach
loop来生成它
我希望它能帮助你 “locatie”:[“1”:{“latitudine”:37.4254,“longitudine”:-122.08,“tip_problema”:“Semafor nefunctional”}]类似于此格式。您还没有使用JSONArray,它是一个JSONObject。@ViralPatel关于如何更改.php以获得该响应的任何提示?我是
PHP
问题的初学者,无法获得返回locatie
between[]的响应需要帮助,请询问。有关如何更改.PHP以获得该响应的提示?我是PHP
事务的初学者,在[]之间无法得到返回locatie
的响应@jarvis@Costin我对PHPYes一点也不了解它实际上帮助了我!我用这种方法在中实现了数组,现在它开始工作了!谢谢
<?php
// Declare two dimensional associative
// array and initilize it
$arr['locatie'] [] = array (
1=>array(
"latitudine"=>1,
"longitudine"=>"Doorbell",
"tip_problema"=>199
),
2=>array(
"latitudine"=>2,
"longitudine"=>"Bottle",
"tip_problema"=>99
),
3=>array(
"latitudine"=>3,
"longitudine"=>"Washing Machine",
"tip_problema"=>7999
)
);
// Function to convert array into JSON
echo json_encode($arr);
?>
{"locatie":[{"1":{"latitudine":1,"longitudine":"Doorbell","tip_problema":199},"2":{"latitudine":2,"longitudine":"Bottle","tip_problema":99},"3":{"latitudine":3,"longitudine":"Washing Machine","tip_problema":7999}}]}