2016-03-31 47 views
-1

我需要在我的PHP代碼中解析JSON。在PHP中解析JSON以提取值

一個簡單的示例....

$myJSON_string = '{ 
    "features": [{ 
      "type": "Feature", 
      "geometry": { 
       "type": "Point", 
       "coordinates": [7.671718, 44.912186] 
      }, 
      "properties": { 
       "ca": 183.5372, 
       "key": "xyz" 
      } 
     }, { 
      "type": "Feature", 
      "geometry": { 
       "type": "Point", 
       "coordinates": [7.685436, 44.921234] 
      }, 
      "properties": { 
       "ca": 183.5372, 
       "key": "kwh" 
      } 
     } 

    ], 
    "type": "FeatureCollection" 
}'; 



    $myJSON_json = json_decode($myJSON_string,true); 

    foreach ($myJSON_json as $f) { 
     echo $f['features']['properties'][0]['key'].'<br />'; 
    } 

我想提取「鍵」參數的值,所以這個樣品中

xyz 
kwh 

我是相當新手PHP,很抱歉....任何建議/示例/替代?

非常感謝您提前!

切薩雷

+2

只要做一個'print_r($ myJSON_json);'你會看到結構 – RiggsFolly

回答

3

您需要遍歷數組features

foreach ($myJSON_json['features'] as $f) { 
    echo $f['properties']['key'].'<br />'; 
}