如何在php中使用嵌套对象创建响应json?

时间:2014-11-06 10:18:58

标签: php mysql json nested

我想用嵌套的json对象创建一个json响应,并按id_hotel

对我的实际结果进行分组

enter image description here

我的实际JSON回应

 {
    "tag": "cities",
    "success": 1,
    "error": 0,
    "items": 2,
    "item": [
        {
            "id": "6194",
            "year": "2013",
            "city": "London",
            "start": "1",
            "id_hotel": "20001",
            "name": "hotel piccadilly",
            "address": "road 4",
            "number_fr": "7003",
            "district": "london city",
            "pr": "GB",
            "fres": "1",
            "mode": "Night",
            "pos": "402",
            "pes": "33",
            "pis": "21",
            "pus": "456"
        },
        {
            "id": "6194",
            "year": "2013",
            "city": "London",
            "start": "1",
            "id_hotel": "20001",
            "name": "hotel piccadilly",
            "address": "road 4",
            "number_fr": "7003",
            "district": "london city",
            "pr": "GB",
            "fres": "1",
            "mode": "Day",
            "pos": "0",
            "pes": "1",
            "pis": "0",
            "pus": "1"
        }
    ]
}

我的PHP代码用于编码响应

        $query = "SELECT * FROM cities WHERE city = 'London'";
    $result = mysql_query($query) or die(mysql_error());
    // check for empty result
    if (mysql_num_rows($result) > 0) {

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

        while ($row = mysql_fetch_array($result)) {
            // temp user array
            $product = array();
            $product["id"] = $row["id"];
            $product["year"] = $row["year"];
            $product["city"] = $row["city"];
            $product["start"] = $row["start"];
            $product["id_hotel"] = $row["id_hotel"];
            $product["name"] = $row["name"];
            $product["address"] = $row["address"];
            $product["number_fr"] = $row["number_fr"];
            $product["district"] = $row["district"];
            $product["pr"] = $row["pr"];
            $product["fres"] = $row["fres"];
            $product["mode"] = $row["mode"];
            $product["pos"] = $row["pos"];
            $product["pes"] = $row["pes"];
            $product["pis"] = $row["pis"];
            $product["pus"] = $row["pus"];              

            // push single product into final response array
            array_push($response["item"], $product);
        }
        // success
        $response["items"] = mysql_num_rows($result);
        $response["success"] = 1;

        // echoing JSON response
        echo json_encode($response);

我渴望的是什么

    {
"tag": "cities",
"success": 1,
"error": 0,
"items": 2,
"item": [
    {
        "id": "6194",
        "year": "2013",
        "city": "London",
        "start": "1",
        "id_hotel": "20001",
        "name": "hotel piccadilly",
        "address": "road 4",
        "number_fr": "7003",
        "district": "london city",
        "pr": "GB",
        "fres": "1",
        "mode": [{
        "value" : "Night",
        "pos": "402",
        "pes": "33",
        "pis": "21",
        "pus": "456"
        },
       { "value" : "Day",
        "pos": "0",
        "pes": "1",
        "pis": "0",
        "pus": "1"
        }]

    }
]
}

提前感谢您的关注!!

2 个答案:

答案 0 :(得分:0)

在函数中添加选项JSON_FORCE_OBJECT。您可以在http://tr2.php.net/manual/en/json.constants.php中了解相关信息。 json_encode($ response,JSON_FORCE_OBJECT);

答案 1 :(得分:0)

1,我建议你做一个小改动来保存一些线路,因此,我添加一些代码来实现你想要的东西

 $query = "SELECT * FROM cities WHERE city = 'London'";
    $result = mysql_query($query) or die(mysql_error());
    // check for empty result
    if (mysql_num_rows($result) > 0) {

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

       $current_id = NULL;

        while ($row = mysql_fetch_array($result)) {  
            if ( $row->id_hotel !== $current_id ){
                $response["item"][] = $row;
                $response["item"]["mode"] = array();
            }
            $current_item = end($response["item"]);
            $current_item["mode"][] = array("value"=>$row->value, "pes"=>$row->pes); 
            $current_id = $row->id_hotel;
        }
        // success
        $response["items"] = mysql_num_rows($result);
        $response["success"] = 1;

        // echoing JSON response
        echo json_encode($response);

让我知道它是否有效。

注意:为了测试目的,只使用了1个变量用于模式,并没有取消我放入模式数组的变量,但应该足以看出主要思想是否有效。感觉有点懒:P