miaotaizi
V2EX  ›  PHP

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

  •  
  •   miaotaizi · Oct 23, 2015 · 3615 views
    This topic created in 3944 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
        1
    marssun  
       Oct 23, 2015
    没有截图..没有具体情况
    paw
        2
    paw  
       Oct 23, 2015
    默认编码格式问题吧
    cxbig
        3
    cxbig  
       Oct 23, 2015
    编码问题,请确认放进 json_decode 的 json 字符串是 utf8 。
    miaotaizi
        4
    miaotaizi  
    OP
       Oct 23, 2015
    @marssun
    @paw
    @cxbig
    刚刚解决了, 我在 decode 之前,把字串里面的换行替换成<br/> 然后就没问题了.
    但是为何会有这样的问题呢?
    miaotaizi
        5
    miaotaizi  
    OP
       Oct 23, 2015
    是因为 \n 或\r 在双引号里面的原因吗?
    yangg
        6
    yangg  
       Oct 23, 2015
    json 字符串是自己拼接的吧,请使用数组 + json_encode
    holyghost
        7
    holyghost  
       Oct 23, 2015
    json 标准里面不允许换行符
    holyghost
        8
    holyghost  
       Oct 23, 2015
    json 必须 utf-8 encoded
    这都是必踩的坑啊
    shoaly
        9
    shoaly  
       Oct 23, 2015
    永远不要自己拼 json, 字符串拼接的才艺不用在 json 中展示
    miaotaizi
        10
    miaotaizi  
    OP
       Oct 24, 2015
    @yangg
    @holyghost
    @shoaly
    感谢大家,学习到了,谢谢.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   949 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 19:30 · PVG 03:30 · LAX 12:30 · JFK 15:30
    ♥ Do have faith in what you're doing.