2014-07-27 134 views
1

我正在嘗試使用以JSON格式輸出結果的API。在JSON的輸出是這樣的:提取JSON數據並嵌入到HTML

"status": "ok", 
"registry": { 
    "id": 110915, 
    "name": "John", 
    "enlisted": 1359114500 
} 

從這個輸出,我想提取name和使用PHP/HTML打印。這個怎麼做?我嘗試了像iframe之類的東西,但那不起作用。

+0

registry.name將具有值約翰... –

回答

1

要打印的名稱,你只需要做到這一點

$json = json_decode(@file_get_contents('file.json')); 
echo $json->registry->name; 

但要注意,你的JSON代碼應該是這樣的

{ 
    "status": "ok", 
    "registry": 
    { 
     "id": 110915, 
     "name": "John", 
     "enlisted": 1359114500 
    } 
} 

你應該把你的代碼放在{}

+0

工作完美!謝謝 – Chris

+0

不客氣:) – Khalid

2

,因爲你要使用的PHP:

<?php 


// get the json 

$data = file_get_contents('http://url_to_the_json'); 


// decode the json 

$data = json_decode($data); 


// see it 

echo '<pre>' . print_r($data, true) . '</pre>'; 


// data is now a php object/array/combination. 
// I'm not sure how your json file is structured but you can try: 

echo $data->registry->name; 
+0

謝謝!這工作。任何只打印'name'的方法? – Chris

+0

編輯答案:-) – rlatief