drmeu26880 2015-06-05 04:24
浏览 26
已采纳

无法在php中解码dota项架构json

I recently got the Dota 2 item schema by following this guide: http://roshpit.ghost.io/getting-updated-item-schema-for-dota2-despite-app570econ-api-being-down-for-5-months/

Since the file is really large, every time I attempt to verify it with json verifiers such as JSONLint, they stop responding. It looks like proper json, and according to the guide it should be proper json. My problem is that when I use json_decode() in PHP it ends up returning a null or empty.

My guess is that it includes some type of character that is not supported by PHP and thus causing it to return null. I read something regarding UTF8 BOM, but I'm not sure if this is the problem.

Could someone help me identify what's wrong with it?

I have used var_dump() to make sure that the contents of the object that I am trying to decode actually has something.

I have also tried removing the UTF8 BOM characters by using the following php function

function remove_utf8_bom($text)
{
     $bom = pack('H*','EFBBBF');
     $text = preg_replace("/^$bom/", '', $text);
     return $text;
}

And I also tried removing the UTF8 BOM characters using this awk script

awk 'NR==1{sub(/^\xef\xbb\xbf/,"")}{print}' INFILE > OUTFILE

Neither of the UTF8 BOM removals seemed to have any effect.

The following is the json file in question:

Original JSON obtained through guide listed above

  • 写回答

1条回答 默认 最新

  • duanba7653 2015-06-05 18:21
    关注

    I ended up fixing the problem by first finding out what error was being displayed through the use of json_last_error(). It turned out to be 5, which is a UTF8 error. This means that my original thought of the UTF8 formatting was incorrect, but I was not properly fixing it before. In the end, I just ended up using the following to fix it:

    json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', file_get_contents("items_game.json")), true );
    

    This basically just got rid of all of the UTF8 characters that were not supposed to be there when trying to decode the file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥15 QFILHelper怎么恢复全字库,提示进程已完成,只能恢复分区文件
  • ¥150 求 《小魔指》街机游戏机整合模拟软件
  • ¥20 你好,我想问下easyExcel下拉多选,或者复选框可以实现吗
  • ¥20 双非跨考工科哪个专业和方向就业前景好?