Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中生成正确的json数据_Php_Mysql_Json_Jsonp - Fatal编程技术网

如何在php中生成正确的json数据

如何在php中生成正确的json数据,php,mysql,json,jsonp,Php,Mysql,Json,Jsonp,我试图使用json_encode()函数在php中生成json数据。然后我将在客户端使用这些数据。我演了一些角色。我当前的代码以这种格式显示json数据 { "data": { "tag":"home", "success":1, "error":0, "uid":"4fc8f94f1a51c5.32653037", "name":"Zafar Saleem", "profile_photo":"http:\/\/example.info\/and

我试图使用json_encode()函数在php中生成json数据。然后我将在客户端使用这些数据。我演了一些角色。我当前的代码以这种格式显示json数据

{
"data":
{
    "tag":"home",
    "success":1,
    "error":0,
    "uid":"4fc8f94f1a51c5.32653037",
    "name":"Zafar Saleem",
    "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile1.jpg",
    "places":
    {
        "place_photo":"http:\/\/example.info\/android\/places_photos\/place1.jpg",
        "created_at":"2012-06-02 00:00:00",
        "seeked":"0"
    }
}
}
{
"data":
{
    "tag":"home",
    "success":1,
    "error":0,
    "uid":"4fc9c413554104.22444656",
    "name":"Name",
    "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile2.jpg",
    "places":
    {
        "place_photo":"http:\/\/example.info\/android\/places_photos\/place2.jpg",
        "created_at":"2012-06-03 00:00:00",
        "seeked":"0"
    }
}
}
{
"data":
{
    "tag":"home",
    "success":1,
    "error":0,
    "uid":"4fc9c48c529675.45551665",
    "name":"Name",
    "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile3.jpg",
    "places":
    {
        "place_photo":"http:\/\/example.info\/android\/places_photos\/place3.jpg",
        "created_at":"2012-06-04 00:00:00",
        "seeked":"20"
    }
}
}
我想在这个表格中显示以上数据的内容

{
"data": 
[
    {
        "tag":"home",
        "success":1,
        "error":0,
        "uid":"4fc8f94f1a51c5.32653037",
        "name":"Zafar Saleem",
        "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile1.jpg",
        "places":
        {
            "place_photo":"http:\/\/example.info\/android\/places_photos\/place1.jpg",
            "created_at":"2012-06-02 00:00:00",
            "seeked":"0"
        }
    },
    {
        "tag":"home",
        "success":1,
        "error":0,
        "uid":"4fc9c413554104.22444656",
        "name":"Name",
        "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile2.jpg",
        "places":
        {
            "place_photo":"http:\/\/example.info\/android\/places_photos\/place2.jpg",
            "created_at":"2012-06-03 00:00:00",
            "seeked":"0"
        }
    },
    {
        "tag":"home",
        "success":1,
        "error":0,
        "uid":"4fc9c48c529675.45551665",
        "name":"Name",
        "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile3.jpg",
        "places":
        {
            "place_photo":"http:\/\/example.info\/android\/places_photos\/place3.jpg",
            "created_at":"2012-06-04 00:00:00",
            "seeked":"20"
        }
    }
]
}
下面是生成json数据的php代码

从数据库获取数据的数据库函数

 public function getHome() {
    $result = mysql_query("SELECT * FROM places") or die(mysql_error());
    // check for result
    $no_of_rows = mysql_num_rows($result);
    if ($no_of_rows > 0) {
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $data[] = $row;
        }
        return $data;
        /*
        $result = mysql_fetch_array($result);
        return $result;
        */
    } else {
        // user not found
        return false;
    }
}
这里是我在php上制作json的地方

if($db->getHome()) {
        $data = $db->getHome();
        foreach($data as $r) {
            $response['success'] = 1;
            $response['uid'] = $r['uid'];
            $response['name'] = $r['name'];
            $response['profile_photo'] = $r['profile_photo_path'];
            $response['places']['place_photo'] = $r['place_photo_path'];
            $response['places']['latitude'] = $r['latitude'];
            $response['places']['longitude'] = $r['longitude'];
            $response['places']['created_at'] = $r['created_at'];
            $response['places']['seeked'] = $r['total_completed'];
            echo json_encode(array('data' => $response));
        }
    } else {
        $response['error'] = 1;
        $response['error_msg'] = 'No data available';
        echo json_encode($response);
    }

请考虑使代码如下所示:

// this function turns a database row into data for frontend use
function convert_to_response($r)
{
  return array(
    'success' => 1,
    'uid' => $r['uid'],
    'name' => $r['name'],
    'profile_photo' => $r['profile_photo_path'],
    'places' => array(
      'place_photo' => $r['place_photo_path'],
      'latitude' => $r['latitude'],
      'longitude' => $r['longitude'],
      'created_at' => $r['created_at'],
      'seeked' => $r['total_completed'],
    ),
  );
}

if($db->getHome()) {
        $data = $db->getHome();
        echo json_encode(array(
            'data' => array_map('convert_to_response', $data) // convert data array
        ));
    } else {
        $response['error'] = 1;
        $response['error_msg'] = 'No data available';
        echo json_encode($response);
    }
您的代码自己回显每个数据行,所以这就是为什么您没有看到数组(使用方括号)

我还将响应转换移到一个单独的函数中,从而使代码更加清晰;通过这种方式,诸如
array\u map
之类的函数可以使用它将一种格式的数据数组转换为另一种格式


希望有帮助。

当我需要将json数据打印到客户端时,我所做的就是这样

print_json(array('logged'=>true),true);
print_json函数将打印第一个对象中传递的对象的json格式的数据。我的第二个论点决定了是否死亡,这是我通常提供的

function print_json($data,$die){
    header('Content-Type: application/json; charset=utf8');
    print_r(json_encode($data));
    if ($die === true) die();
}

小菜一碟。希望您喜欢。这是我多年来想出的最好的方法。

我看不出您的代码有任何问题。然后,在获取JSON数据后,可以在客户端使用JSON.parse()。如何以想要的格式显示JSON?目前它的显示方式有所不同,我不需要说明这其实很奇怪,因为
json_encode()
将数组用方括号括起来。为什么在这种情况下它不这样做呢?没有测试(所以不作为答案),但如果您将“return$data;”更改为“return array$data;”(getHome()的中间行),它会起作用吗?