【问题标题】:json_decode + json_encode combo not creating original JSONjson_decode + json_encode 组合不创建原始 JSON
【发布时间】:2012-03-19 07:51:51
【问题描述】:

我有一些通过 API 调用获得的 JSON,我在其上运行 json_decode,从中获取一个数组,然后使用 json_encode 对其进行重新编码。然而,结果不是相同的 JSON;它弄乱了 URL。如何让它正确编码?

原创

{"created_at":"Mon, 19 Mar 2012 01:34:41 +0000","entities":{"hashtags":[{"text":"stanford","indices":[23,32]}],"urls":[{"url":"http:\/\/t.co\/Of4z6jKG","expanded_url":"http:\/\/360.io\/5sZc2T","display_url":"360.io\/5sZc2T","indices":[33,53]}],"user_mentions":[]},"from_user":"rayfk","from_user_id":335143881,"from_user_id_str":"335143881","from_user_name":"Raymond Kennedy","geo":{"coordinates":[37.4227,-122.1753],"type":"Point"},"id":181554251733020673,"id_str":"181554251733020673","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/1468102095\/image_normal.jpg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1468102095\/image_normal.jpg","source":"<a href="http:\/\/www.occipital.com\/360\/app" rel="nofollow">360 Panorama<\/a>","text":"View from mid lake log #stanford http:\/\/t.co\/Of4z6jKG","to_user":null,"to_user_id":null,"to_user_id_str":null,"to_user_name":null}

解码/编码组合后

{"created_at":"Mon, 19 Mar 2012 01:34:41 +0000","entities":{"hashtags":[{"text":"stanford","indices":[23,32]}],"urls":[{"url":"http:\/\/t.co\/Of4z6jKG","expanded_url":"http:\/\/360.io\/5sZc2T","display_url":"360.io\/5sZc2T","indices":[33,53]}],"user_mentions":[]},"from_user":"rayfk","from_user_id":335143881,"from_user_id_str":"335143881","from_user_name":"Raymond Kennedy","geo":{"coordinates":[37.4227,-122.1753],"type":"Point"},"id":181554251733020673,"id_str":"181554251733020673","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/1468102095\/image_normal.jpg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1468102095\/image_normal.jpg","source":"<a href="http:\/\/www.occipital.com\/360\/app" rel="nofollow">360 Panorama<\/a>","text":"View from mid lake log #stanford http:\/\/t.co\/Of4z6jKG","to_user":null,"to_user_id":null,"to_user_id_str":null,"to_user_name":null}

这些是完整的 sn-ps,但罪魁祸首是:

原创 "source":"&amp;lt;a href=&amp;quot;http:\/\/www.occipital.com\/360\/app&amp;quot; rel=&amp;quot;nofollow&amp;quot;&amp;gt;360 Panorama&amp;lt;\/a&amp;gt;"

之后 "source":"&lt;a href="http:\/\/www.occipital.com\/360\/app" rel="nofollow"&gt;360 Panorama&lt;\/a&gt;"

【问题讨论】:

  • 究竟是什么“搞砸了”?我无法自发发现差异。
  • 刚刚添加;对此感到抱歉
  • 它也可能不是格式错误...我使用这个工具查看 JSON,它说它是一个坏变量:jsonviewer.stack.hu
  • 那些是json_encode 根本不应该接触的 HTML 实体。确定您没有通过htmlentities 或类似的方式输入这些字符串?
  • 你用过htmlentities()吗?

标签: php json


【解决方案1】:

我不确定是什么原因造成的,但您可以通过将html_entity_decode() 函数应用于 after 版本来纠正它。这会将 &amp;lt;&amp;quot; 等内容更改回其原始形式。

根据它如何影响您的引用,您也可以传递一些标志以获得所需的结果。

  • ENT_COMPAT:将转换双引号并保留单引号。
  • ENT_QUOTES:将转换双引号和单引号。
  • ENT_NOQUOTES:不转换双引号和单引号。

[编辑]

通过这个函数运行你损坏的 JSON:

function fixDoubleQuotedJSON($broken_json)
{
   return str_replace('"','\\"',$broken_json);
}

【讨论】:

  • 所以实际上是相反的......我使用的工具接受编码的HTML并拒绝解码。 JSON 需要编码的 HTML 还是这个工具很糟糕?
  • jsonformatter.curiousconcept.com/#jsonformatter 这个工具还说后面的版本不好,在同一行。
  • 不,JSON 不必应用 htmlentities()。虽然,为了避免问题,引号应该被转义。有一个已知问题,双引号被 json_encode() 转义,然后无法被 jQuery 的 .parseJSON() 读取,但如果你用谷歌搜索它们,有一些解决方法。
  • 那么为什么这两个 json 查看器都在这条线上抱怨呢?
  • 哦,没关系,我明白你为什么现在给我报价单上的东西了 :)
【解决方案2】:

http://codepad.org/DMkAS2iR

他们似乎是平等的,除了在一个不例外的地方:

before: "id":181554251733020673,"id_str"
after:  "id":181554251733020000,"id_str"

在“无损” json 转换后,这些 id 不会真正匹配,并且从 PHP 5.4 开始支持 JSON_BIGINT_AS_STRING 选项。

顺便说一下Codepad的php版本是5.2.5;

【讨论】:

  • 不知道是不是和版本有关,我的是5.2.17,无论是解码还是编码都会破坏特殊的HTML字符并再次将它们变成引号,这就是破坏它。跨度>
  • 我也刚刚确认是编码搞砸了。如果我在解码后立即回显,则字符串与原始字符串匹配。
猜你喜欢
  • 2012-03-08
  • 2018-05-21
  • 2011-05-03
  • 2018-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-23
  • 2021-07-12
相关资源
最近更新 更多