mpx / lua-cjson

Lua CJSON is a fast JSON encoding/parsing module for Lua
https://kyne.au/~mark/software/lua-cjson.php
MIT License
924 stars 474 forks source link

Decode JSON went wrong #46

Closed aph12123 closed 7 years ago

aph12123 commented 7 years ago

I have a config file whose content is organized in JSON, when I read the file from disk and assign its content to a Lua string and print shows exactly the same as the file.But when I try to decode it use cjson the console shows "Expected value but found invalid token at character 1".

Here is my file: {"apkname":"lukaYingyongbao_215_jiagu_sign.apk","verName":"2.1.5","verCode":20,"verForced":"false","appname":"撸卡漫画","title":"撸卡漫画更新啦","verInfo":"《撸卡漫画》新版本上线啦!\n主要修复版本部分机型的登录问题以及性能优化"}

aph12123 commented 7 years ago

I found the problem it's the file encoding.My config file was encoded in UTF-8 with BOM by default. When read into memory it has three invisible chars in the head of the string. So it caused the cjson.decode crash.The solution is change file encode to UTF-8 then it works perfectly fine.