【问题标题】:how to make proper json data in php如何在php中制作正确的json数据
【发布时间】:2012-06-07 10:43:20
【问题描述】:

我正在尝试使用 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);
    }

【问题讨论】:

  • 我看不出你的代码有什么问题。然后,您可以在获取 json 数据后在客户端使用 JSON.parse()
  • 如何以我想要的格式显示 json?目前它显示不同,我不需要
  • 这其实很奇怪,因为json_encode() 会用方括号包围数组。
  • 为什么在这种情况下不这样做?
  • 未测试(因此未作为答案),但如果您更改“return $data;”它是否有效到“返回数组($data);” (getHome() 的中线)?

标签: php mysql json jsonp


【解决方案1】:

当我需要向客户端打印 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();
}

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

【讨论】:

    【解决方案2】:

    考虑让代码看起来像这样:

    // 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_map 等函数就可以使用它来将一种格式的数据数组转换为另一种格式。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2013-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-17
      • 2016-03-30
      • 1970-01-01
      • 2019-10-01
      • 2020-06-20
      相关资源
      最近更新 更多