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 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计