2017-06-10 140 views
-2

IM使用JSON新的和有大規模的鬥爭試圖解析此JSON通過JSON字符串PHP解析

{ 
    "730": { 
     "success": true, 
     "data": { 
      "price_overview": { 
       "currency": "USD", 
       "initial": 1499, 
       "final": 1499, 
       "discount_percent": 0 
      } 
     } 
    } 
} 

我曾嘗試不同的方法,但仍在努力獲得的初始值在price_overview

+1

[用PHP解析JSON文件]的可能副本(https://stackoverflow.com/a/4343691/6521116) –

回答

1

你需要json_decode,然後,只需使用生成的dictionary即可獲取該值。就像這樣:

$json = <<< JSON 
    { 
    "730": { 
     "success": true, 
     "data": { 
      "price_overview": { 
       "currency": "USD", 
       "initial": 1499, 
       "final": 1499, 
       "discount_percent": 0 
      } 
     } 
    } 
} 
JSON; 

$json_a = json_decode($json, true); 
echo $json_a['730']['data']['price_overview']['initial']; 

鍵盤

http://codepad.org/i1ELBxd9

進一步閱讀

Using JSON with PHP

+0

謝謝,讚賞 –

+0

@Bradm沒問題,只是在詢問之前嘗試做更多的研究! –

1

試試這個:

<?php 
$data = '{"730":{"success":true,"data":{"price_overview":{"currency":"USD","initial":1499,"final":1499,"discount_percent":0}}}}'; 

$json = json_decode(trim($data), true); 

echo '<pre>'; 
print_r($json[730][data][price_overview]); 
echo '</pre>';