普通网友 2016-03-31 21:36
浏览 148

使用PHP解析JSON并分配给变量

I have the following JSON file that I need to extract a value and assign it to a PHP variable. The value I need is "icn".

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

I have tried the following but get no results.

$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();

Results are

LAST-Error:0
LAST-Error-Msg:No error
  • 写回答

2条回答 默认 最新

  • doulie0178 2016-03-31 21:36
    关注

    I figured out where I went wrong.

    $myjson = file_get_contents('http://testurl.org/info.json');
    $dec = (Array)json_decode($myjson,true);
    $ICN = $dec["data"]["items"][0]["icn"];
    
    评论

报告相同问题?