如何创建JSON响应JSON

2023-09-04 07:32:39 作者:事前烟

我开发一个Android应用程序使用谷歌地图V2,服务器端我使用PHP和MySQL数据库。在数据库中有纬度和经度方面已经有很多位置坐标存储,因此当用户打开应用程序,通过服务器,并显示在地图上的研究背景服务获取所有这些纬度和经度的数据库,但我就是我要的是停留在php结束。假设这是我的数据库中的数据。

I am developing an android app by using google map v2, at server end i am using php and mysql database. In database there are already many location coordinates in terms of latitudes and longitudes are stored, so what i want is when user turn On the app the backgroud service fetch all these latitudes and longitudes from database through server and display on the map, but i am stuck at php end. Suppose this is my database data

+---------+---------+---------+--------+
|  uid    |  lat    | long    |  loc   |
+---------+---------+------- -+--------+
| 1       |Value1 A1|Value2 A1|    A   |
+---------+---------+---------+--------+
| 2       |Value1 B2|Value2 B2|    B   |
+---------+---------+---------+--------+
| 3       |Value1 C3|Value2 C3|    C   |
+---------+---------+---------+--------+
| 4       |Value1 D4|Value2 D4|    D   |
+---------+---------+---------+--------+

此查询可以给我数据行明智 $结果= mysql_query(SELECT UID,纬度,长,LOC FROM表)但我怎么可以存储在该数据一些阵列序列明智的,所以当连接code它JSON就会产生这样的反应相似

This query can give me data row wise $result = mysql_query("SELECT uid, lat, long, loc FROM table") but how can i store this data in some array sequence wise so when encode it in json it will generate similar response like this

    [
    {
    "uid": "1": [
            {
                "lat": "Value1_A1",
                "lon": "Value2_A1"
                "loc": "A"
            }

        ]
    },
    {
    "uid": "2": [
            {
                "lat": "Value1_B2",
                "lon": "Value2_B2"
                "loc": "B"
            }

        ]
    },
{
    "uid": "3": [
            {
                "lat": "Value1_C3",
                "lon": "Value2_C3"
                "loc": "C"
            }

        ]
    },
{
    "uid": "4": [
            {
                "lat": "Value1_D4",
                "lon": "Value2_D4"
                "loc": "D"
            }

        ]
    },
    and so on...............
]

请帮我,我serching从过去两天,但无法找到任何合理的解决方案的解决方案。

Please help me as i am serching for the solution from last two days but unable to find any reasonable solution.

推荐答案

尝试mysql_fetch_assoc:

Try mysql_fetch_assoc:

$json = array();
while ($row = mysql_fetch_assoc($result)) {
    $json[$row['uid']] = array(
       'lat' => $row['lat'],
       'lon' => $row['lon'],
       'loc' => $row['loc']
    );
}
echo json_encode($json);

您使用mysqli或PDO_MYSQL而不是mysql的_

You should use MySQLi or PDO_MySQL instead of mysql_.