php - 谷歌地图 : org. json.JSONArray 无法转换为 json 对象

标签 php android mysql json google-maps

我从 mysql 数据库表中获取纬度和经度的值,其中两个字段的数据类型都是 double 的,在 php 文件中我将这些值放在一个数组中并将它们作为对 json 的响应发送。我的 logcat 显示这些值是正确的,但它也表示

"org.json.JSONArray cannot be converted to json object"

我在 map 上看不到我想要的经纬度结果,但 map 上还有其他地方。这是我的 java 代码部分,它从 php 获取值。那么我如何处理这种转换以获取这些值,因为它们来自 php,这样我就可以看到数据库中的存储位置。

JSONObject json_user1 = new JSONObject(responseFromPHP);

        JSONObject json_user2 = json_user1.getJSONObject("latlong");

        double latitude=Double.parseDouble((json_user2.getString(TAG_LATITUDE)));

        double longitude=Double.parseDouble((json_user2.getString(TAG_LONGITUDE)));
                    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();
            Marker Lahore = map.addMarker(new MarkerOptions().position(
                    new LatLng(latitude, longitude))
                .title("Lahore"));
         // Move the camera instantly to hamburg with a zoom of 15.
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 5));

            // Zoom in, animating the camera.
            map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

LogCAT: enter image description here

10-10 22:49:47.655: D/Show lat n long:(30670): {"success":1,"latlongs":[{"longitude":"74.3436","latitude":"31.5497"}]}
10-10 22:49:47.655: W/System.err(30670): org.json.JSONException: Value [{"longitude":"74.3436","latitude":"31.5497"}] at latlongs of type org.json.JSONArray cannot be converted to JSONObject
10-10 22:49:47.660: W/System.err(30670):    at org.json.JSON.typeMismatch(JSON.java:100)
10-10 22:49:47.660: W/System.err(30670):    at org.json.JSONObject.getJSONObject(JSONObject.java:573)
10-10 22:49:47.660: W/System.err(30670):    at com.DRMS.disas_recovery.MainActivity$LoadMap.onPostExecute(MainActivity.java:80)
10-10 22:49:47.660: W/System.err(30670):    at com.DRMS.disas_recovery.MainActivity$LoadMap.onPostExecute(MainActivity.java:1)
10-10 22:49:47.665: W/System.err(30670):    at android.os.Handler.dispatchMessage(Handler.java:99)

PHP 代码:

<?php


$response = array();


require_once 'include/DB_Functions.php';
    $db = new DB_Functions();

$result = mysql_query("SELECT latitude, longitude FROM tbl_map WHERE map_id=1") or die(mysql_error());



if (mysql_num_rows($result) > 0) {

    $response["latlongs"] = array();

    while ($row = mysql_fetch_array($result)) {

        $latlong = array();
        $latlong["latitude"] = $row["latitude"];
        $latlong["longitude"] = $row["longitude"];
        array_push($response["latlongs"], $latlong);
    }
    $response["success"] = 1;
    // echoing JSON response
    echo json_encode($response);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "Error nothing found";

    // echo no users JSON
    echo json_encode($response);
}
?>

最佳答案

你的问题在这里

JSONObject json_user2 = json_user1.getJSONObject("latlong");

json_user1.get("latlong") 是一个 JSONArray 而不是 JSONObject。您可以分辨出来,因为字符串以 [ 而不是 { 开头,它们分别对应于 ArrayObject

尝试以下操作。

JSONArray json_user2 = json_user1.getJSONArray("latlong");

这应该适合你。

关于php - 谷歌地图 : org. json.JSONArray 无法转换为 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19303197/

相关文章:

android - Android SDK(模拟器)上的 Root 访问

mysql - 从相关表中一次选择多个 select count()

php - 对具有多个索引的 mysql json 对象求和

php - 如何从字符串中提取大写单词

android - 是否可以在 Android 中为图像/图标使用字体

java - 如何在 Android 中将 int 数组转换为 JSON 字符串?

Python存储过程未运行,没有错误

php - 列中的 MySQL 值未在 PHP 变量中返回

php - 不在对象上下文中时使用 $this

php - SQL 最近日期