2016-03-31 12 views
0

値を抽出してPHP変数に割り当てるのに必要な次のJSONファイルがあります。必要な値は "icn"です。PHPでJSONを解析し、変数に代入する

{ 
    "apiVersion": "1.0", 
    "data": { 
    "updated": 20160331141332, 
    "totalItems": 1, 
    "currentItemCount": 1, 
    "items": [ 
     { 
     "birthDate": "20171101", 
     "fullName": "DOE,JOHN D", 
     "icn": "889081784V888383", 
     "pid": "55R1;60004" 
     } 
    ] 
    } 
} 

私は以下を試したが、結果は得られなかった。

$myjson = file_get_contents('http://testurl.org/info.json'); 
print_r(json_decode($myjson->data->items[0]->icn,true)); 

echo "<br>LAST-Error:"; echo json_last_error(); 
echo "<br>LAST-Error-Msg:"; echo json_last_error_msg(); 

結果は

LAST-Error:0 
LAST-Error-Msg:No error 
+3

可能な複製を(http://stackoverflow.com/questions/29308898/how-do -i-extract-data-from-json-with-php) –

答えて

0

ある私が間違っていたところ、私は考え出しました。

​​
0

$myjsonあなたがオブジェクトとして扱い、文字列です:

$myjson->data->items[0]->icn 

あなたはJSON要素を取得することができますあなたはその後、最初デコードJSON文字列、を持っています。

単純に閉じ括弧を移動し、Trueオプション削除:[?どうすればPHPでJSONからデータを抽出します]の

 
print_r(json_decode($myjson->data->items[0]->icn, true)); 
#        ┌──────────────────────────┘ 
print_r(json_decode($myjson)->data->items[0]->icn); 
関連する問題