用PHP搜索JSON

问题描述:

我抓取了一堆JSON数据,我使用php json_decode进行解码。我想搜索或提取JSON对象的某些元素。我已经看了一些类似的例子,但收效甚微。用PHP搜索JSON

JSON结构是这样的。我想使用PHP提取股票元素进行比较。

{ 
"items":{ 
     "item000":{ 
      "color":"black", 
      "skuId":"sku000", 
      "price":{ 
       "sku000":"139.99" 
      }, 
      "name":"item Name", 
      "stock":"in stock" 
     } 
} 
+3

它应该是`$ json ['items'] ['item000'] ['stock']`在它被解析后(假设`$ json = json_decode('...');`)。 – 2011-01-20 21:16:54

根据php.net,如果你使用的东西,如:

$decodedArray = json_decode($jsonString,true); 

并根据您的例子来访问股票价值,你将需要:

foreach($decodedArray['items'] as $itemKey=>$itemProps) 
{ 
    echo $itemProps['stock']; 
} 

正如我测试,你的json无效。你必须添加一个更多的花括号添加你的json的结尾来关闭主根对象:)

+0

太棒了,谢谢。正是我需要的。 – ajay87 2011-01-20 21:37:14