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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程