2017-05-25 78 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); 
?>