2016-01-26 44 views
0

我有一個奇怪的格式的JSON ...但這是我接收它的方式。因爲裏面的數組非常大,所以只需複製和粘貼它需要很長時間,所以我想知道是否有PHP方法來完成它。如何使用PHP「修剪」這個JSON?

的方式,我得到它是這樣的:

{"count":459,"results":[{"title":"Something ....}],"params":{"limit..},"type":"Listing","pagination":{"..":5}} 

但我想只有「結果」陣列的[{"title":"Something ....}]基本組成部分。我會怎麼做?

+0

瘋狂的計劃:http://php.net/manual/en/function.json-decode.php –

+0

你不會破壞json字符串。你將json解碼爲一個本地結構,然後使用本機工具進行破解,然後重新編碼爲json。直接搗毀JSON可能會腐敗它,而最終你什麼都沒有。 –

+0

'$ json = json_decode(「Your-String-Here」)'然後(根據yr代碼)你有一個包含'$ json-> results'的對象作爲數組 – fusion3k

回答

1

$arr = json_decode(your_json, true); 

如果再次定義它爲JSON,做

json_encode($arr['results']); 
0

你可以得到該部分如下:

$json = '{"count":459,"results":[{"title":"Something ...."}],"params":{"limit":".."},"type":"Listing","pagination":{"..":5}}'; 
$obj = json_decode($json); 
echo $obj->results[0]->title; 

輸出:

東西....

0

你必須解碼你的json。確保json是有效的!

您解碼的json返回stdClass而不是數組的對象

以下測試代碼:

$json = '{"count":459,"results":[{"title":"Something ...."}],"params":{"limit": "foo"},"type":"Listing","pagination":{"foo":5}}'; 

$decoded = json_decode($json); 

所以,你可以從對象$decoded訪問數組results

print_r($decoded->results); 

但是,在陣列中,有物體,因此:

echo $decoded->results[0]->title; // Something ....