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 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化