dpzbzp8728 2011-01-22 23:27
浏览 72
已采纳

JSON“正式”支持本机类型的转换吗?

In PHP, you can use json_encode to encode an object as a json string.

$string = json_encode($some_object);

However, PHP has the standard slew of datatypes which are not considered objects (ints, strings, etc.) If you pass in a string to json_encode, this returns a string that contains a javascript statement that could be used to define the string.

In less awkward phrasing, this

echo json_encode("Hello 
world, please don't " . '"' . "misuse quote's for emphasis " .  "or possessive apostrophes' ");

will output this (a javascript ready string)

"Hello 
\tworld, please don't \"misuse quote'sor possessive apostrophes' "

Is this behavior part of the JSON specification? That is, does JSON define or recommend how an implementation should handle conversion of native, non-object, datatypes? Or even have an opinion on conversion at all? My reading of the RFC left this as ambiguous, but I'm crap at interpreting these things.

I ask because I'm interested in the likelihood of this behavior disappearing from a future version of the function. i.e. If it's codified in a specification somewhere, it's less likely to disappear than if it was a one off someone thought to add on during development.

  • 写回答

2条回答 默认 最新

  • doubu4406 2011-01-22 23:31
    关注

    JSON doesn't care about native types at all. It is up to the developer of the JSON library or functionality as to how the JSON is translated to and from types that the programming language can use/understand.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料