2012-06-02 44 views
1

我正在嘗試使用json_encode()函數在php中創建json數據。然後我會在客戶端使用這些數據。我嘗試了一些部分。我當前的代碼顯示這種格式的JSON數據如何在php中製作正確的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; 
    } 
} 

這裏的數據是我使JSON在PHP

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); 
    } 
+1

我沒有看到你的代碼有什麼問題。然後,您可以在客戶端使用JSON.parse()獲取json數據 –

+0

如何以我想要的格式顯示json?目前它顯示的不一樣,我不需要 – 2619

+0

這實際上很奇怪,因爲'json_encode()'將包圍帶有方括號的數組。 –

回答

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之類的函數用來將一種格式的數據陣列轉換成另一種格式。

希望它有幫助。

0

我只需要將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(); 
} 

一塊蛋糕。希望你喜歡它,這是多年來我提出的最好的方法。