miaotaizi
V2EX  ›  PHP

同样一截 json 字符串,在两台机器上使用 json_decode 会得到不同的结果?

  •  
  •   miaotaizi · Oct 23, 2015 · 3786 views
    This topic created in 4034 days ago, the information mentioned may be changed or developed.
    为何我用 json_decode 的时候 一台机器上的 中文 没问题,但是到另外一台机器上 执行 json_last_error() 会得到错误, 两台机器上的 php 版本相同.
    10 replies  •  2015-10-24 20:54:43 +08:00
    marssun
    marssun
    Oct 23, 2015
    1
    没有截图..没有具体情况
    paw
    paw
    Oct 23, 2015
    2
    默认编码格式问题吧
    cxbig
    cxbig
    Oct 23, 2015
    3
    编码问题,请确认放进 json_decode 的 json 字符串是 utf8 。
    miaotaizi
    miaotaizi
    OP
    Oct 23, 2015
    4
    @marssun
    @paw
    @cxbig
    刚刚解决了, 我在 decode 之前,把字串里面的换行替换成<br/> 然后就没问题了.
    但是为何会有这样的问题呢?
    miaotaizi
    miaotaizi
    OP
    Oct 23, 2015
    5
    是因为 \n 或\r 在双引号里面的原因吗?
    yangg
    yangg
    Oct 23, 2015
    6
    json 字符串是自己拼接的吧,请使用数组 + json_encode
    holyghost
    holyghost
    Oct 23, 2015
    7
    json 标准里面不允许换行符
    holyghost
    holyghost
    Oct 23, 2015
    8
    json 必须 utf-8 encoded
    这都是必踩的坑啊
    shoaly
    shoaly
    Oct 23, 2015
    9
    永远不要自己拼 json, 字符串拼接的才艺不用在 json 中展示
    miaotaizi
    miaotaizi
    OP
    Oct 24, 2015
    10
    @yangg
    @holyghost
    @shoaly
    感谢大家,学习到了,谢谢.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   768 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 23:35 · PVG 07:35 · LAX 15:35 · JFK 18:35
    ♥ Do have faith in what you're doing.