2

I broke the code up into as many steps as possible to try to figure out what it was doing.

         $addsData='http://football.myfantasyleague.com/2013/exportTYPE=topAdds&L=&W=&JSON=1';                

         $addsData = json_encode(file_get_contents($addsData));

         $addersData = file_put_contents("addsData.txt", $addsData);

         $getAdds = file_get_contents("addsData.txt");

         $topAddsData = json_decode($getAdds, true);

         echo "<pre>";
         print_r($topAddsData);
         echo "</pre>";

And Here is the result that I am getting...

"version":"1.0","topAdds":{"week":"1","player":[{"percent":"25.95","id":"9705"},{"percent":"23.92","id":"10372"},{"percent":"23.72","id":"11440"},{"percent":"23.43","id":"11259"},{"percent":"20.29","id":"9079"},{"percent":"17.06","id":"10048"},{"percent":"16.93","id":"11227"},{"percent":"14.66","id":"10500"},{"percent":"14.10","id":"9964"},{"percent":"13.90","id":"10862"},{"percent":"13.80","id":"9834"},{"percent":"13.55","id":"10355"},{"percent":"13.29","id":"10961"},{"percent":"13.27","id":"9437"},{"percent":"11.87","id":"9912"},
}

My understanding from reading the manual is that it should be an Associative array because I am passing in the true parameter. What am I missing here?

4

4 回答 4

5

您正在查询的 URL 已经返回一个 JSON 字符串(这可以从 URL 参数中看出),然后您将再次对其进行编码。结果,之后的解码仍然会给您留下 JSON 字符串。

例如,如果 URL 的内容(以及 的初始值$addsData)正在描述一个对象:

{ "foo": "bar" }

然后在编码后的新值$addsData是描述一个字符串:

"{ \"foo\": \"bar\" }"

跳过json_encode,事情应该可以正常工作。

于 2013-09-03T22:25:41.320 回答
1

您正在对数据进行编码,然后再次对其进行解码。不要重新编码数据。按原样使用它。更改此行:

$addsData = json_encode(file_get_contents($addsData));

对此:

$addsData = file_get_contents($addsData);
于 2013-09-03T22:26:01.067 回答
1

可能您的数据不是有效的 JSON。您可以在这里查看:http: //jsonlint.com/ 您的代码在有效的 JSON 文件上运行良好。

于 2013-09-03T22:29:13.553 回答
1

一个简单的解决方案是

$arrJson = json_decode('http://football.myfantasyleague.com/2013/exportTYPE=topAdds&L=&W=&JSON=1');
print_r($arrJson);

这应该将结果作为数组提供。

于 2013-09-03T22:31:58.443 回答