Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从php解析json_Php_Json - Fatal编程技术网

无法从php解析json

无法从php解析json,php,json,Php,Json,我有这样一个JSON: { "avatar": [ { "Trophies": 1022, "clanLevel": 1, "Attack K Factor": -1921682810, "Attacks Won": 1, "freeGems": -1916837036, "clanBadge": 0, "clanCaslteLevel": 5, "currentHomeId": 1288

我有这样一个JSON:

{
  "avatar": [
    {
      "Trophies": 1022,
      "clanLevel": 1,
      "Attack K Factor": -1921682810,
      "Attacks Won": 1,
      "freeGems": -1916837036,
      "clanBadge": 0,
      "clanCaslteLevel": 5,
      "currentHomeId": 12888426248,
      "clanRole": 2,
      "exp": 5013,
      "homeId": 12888426248,
      "Attacks Lost": -1307141699,
      "clanId": 326417604098,
      "boughtGems": -1517098100,
      "userNameChange": false,
      "numOfNameChanges": 0,
      "level": 111,
      "league": 5,
      "userName": "King Shiv",
      "nameTag": 1440968203000,
      "clanName": "lol",
      "Defenses Won": 17,
      "maxCcTroops": 30,
      "gems": -1370568149,
      "Defenses Lost": -2055915376,
      "townHall": 9,
      "inWar": 1,
      "Attack Rating": -1000115629
    }
  ]
}
$url = "http://185.112.249.77:9999/Api/Player?player=1;
$url = preg_replace("/ /", "%20", $url);
$jsondata = file_get_contents($url);
$data = json_decode($jsondata, true);
echo "IGN: ".$data['avatar']['userName'];
echo "<br />Town Hall: ".$data['avatar']['townHallLevel'];
echo "<br />Level: ".$data['avatar']['level'];
echo "<br />Trophies: ".$data['avatar']['trophies'];
echo "<br />".$data['avatar']['clanRole'];
我试着这样解析它:

{
  "avatar": [
    {
      "Trophies": 1022,
      "clanLevel": 1,
      "Attack K Factor": -1921682810,
      "Attacks Won": 1,
      "freeGems": -1916837036,
      "clanBadge": 0,
      "clanCaslteLevel": 5,
      "currentHomeId": 12888426248,
      "clanRole": 2,
      "exp": 5013,
      "homeId": 12888426248,
      "Attacks Lost": -1307141699,
      "clanId": 326417604098,
      "boughtGems": -1517098100,
      "userNameChange": false,
      "numOfNameChanges": 0,
      "level": 111,
      "league": 5,
      "userName": "King Shiv",
      "nameTag": 1440968203000,
      "clanName": "lol",
      "Defenses Won": 17,
      "maxCcTroops": 30,
      "gems": -1370568149,
      "Defenses Lost": -2055915376,
      "townHall": 9,
      "inWar": 1,
      "Attack Rating": -1000115629
    }
  ]
}
$url = "http://185.112.249.77:9999/Api/Player?player=1;
$url = preg_replace("/ /", "%20", $url);
$jsondata = file_get_contents($url);
$data = json_decode($jsondata, true);
echo "IGN: ".$data['avatar']['userName'];
echo "<br />Town Hall: ".$data['avatar']['townHallLevel'];
echo "<br />Level: ".$data['avatar']['level'];
echo "<br />Trophies: ".$data['avatar']['trophies'];
echo "<br />".$data['avatar']['clanRole'];
它不返回任何值。为什么会这样

它只是返回:

签名: 大会堂: 级别: 奖杯:

使用json\u解码,第二个参数设置为true将返回数组 $data['avatar']有子数组,所以您可以像访问整个子数组一样访问它

$data = json_decode($json,true);
$subarray = $data['avatar'][0];
echo $subarray['userName']; // King Shiv

请参见

您的url似乎有问题。使用cURL进行了尝试,但返回的结果是无法连接到主机,主机可能是防火墙,阻止了您的配置。你的json解码很好,解决地址问题,你就会得到内容。

这是我正在尝试的页面:@shivanpaw此URL不提供json数据。它返回html内容,那么,你如何解码它们呢?谢谢,我现在得到了:D@sandepsureyou忘了用