2017-05-25 120 views
0

我该如何解码和回显这个维基百科的'networth'key-valueJSON提取维基百科JSON密钥php

<?php 
    $json_string = 'https://en.wikipedia.org/w/api.php?action=query&titles=Jeff_Bezos&prop=revisions&rvprop=content&rvsection=0&format=json'; 
    $jsondata = file_get_contents($json_string); 
    $obj = json_decode($jsondata,true); 
?> 
+0

如果你更详细地解释你到目前为止的代码问题会有帮助吗? – ADyson

+0

所需的“networth”不是JSON对象。你必须做字符串查找只。 – Akilan

回答

0

我认为这应该工作。

<?php 
    $json_string = 'https://en.wikipedia.org/w/api.php?action=query&titles=Jeff_Bezos&prop=revisions&rvprop=content&rvsection=0&format=json'; 
    $jsondata = file_get_contents($json_string); 
    $obj = json_decode($jsondata,true); 
    $temp = $obj['query']['pages']['142528']['revisions'][0]['*']; 
    $pos = strpos($temp, 'networth'); 
    for($x = $pos; ; $x++){ 
     if($temp[$x] == '=') 
      $y = $x+1; 
     if($temp[$x] == '}' && $temp[$x+1] == '}') 
      break; 
    } 
    $networthValue = trim(substr($temp, $y, $x-$y)," "); 
    print_r($networthValue); 
?>