[英]json_decode + json_encode combo not creating original JSON
我有一些通过API调用获得的JSON,我在其上运行json_decode
,从中获取一个数组,然后使用json_encode
重新对其进行编码。 但是结果不是相同的JSON。 弄乱了网址。 如何使其正确编码?
原版的
{"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}
这些是完整的摘要,但罪魁祸首是:
原始的 "source":"<a href="http:\\/\\/www.occipital.com\\/360\\/app" rel="nofollow">360 Panorama<\\/a>"
在 "source":"<a href="http:\\/\\/www.occipital.com\\/360\\/app" rel="nofollow">360 Panorama<\\/a>"
我不知道是什么原因造成,但你可以通过应用纠正html_entity_decode()版后功能的。 这会改变诸如<
或"
回到其原始形式。
根据它对报价的影响方式,您可以传递一些标志来获得所需的结果。
[编辑]
通过此函数运行损坏的JSON:
function fixDoubleQuotedJSON($broken_json)
{
return str_replace('"','\\"',$broken_json);
}
他们似乎是平等的,除了在一个例外的地方:
before: "id":181554251733020673,"id_str"
after: "id":181554251733020000,"id_str"
经过“无损” json转换后,这些id不会真正匹配,并且PHP 5.4支持JSON_BIGINT_AS_STRING
选项。
顺便说一下,Codepad的php版本是5.2.5。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.