javascript 从键上的 json_encoded 字符串中删除双引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8837659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove double-quotes from a json_encoded string on the keys
提问by DarkMantis
I have a json_encoded array which is fine.
我有一个很好的 json_encoded 数组。
I need to strip the double-quotes on all of the keys of the json string on returning it from a function call.
我需要在从函数调用返回它时去掉 json 字符串的所有键上的双引号。
How would I go about doing this and returning it successfully?
我将如何去做并成功返回它?
Thanks!
谢谢!
I do apologise, here is a snippet of the json code:
我很抱歉,这是 json 代码的一个片段:
{"start_date":"2011-01-01 09:00","end_date":"2011-01-01 10:00","text":"test"}
Just to add a little more info:
只是添加更多信息:
I will be retrieving the JSON via an AJAX request, so if it would be easier, I am open to ideas in how to do this on the javascript side.
我将通过 AJAX 请求检索 JSON,所以如果它更容易,我愿意接受如何在 javascript 端执行此操作的想法。
回答by DaveRandom
EDITEDas per anubhava's comment
根据anubhava的评论编辑
$str = '{"start_date":"2011-01-01 09:00","end_date":"2011-01-01 10:00","text":"test"}';
$str = preg_replace('/"([^"]+)"\s*:\s*/', ':', $str);
echo $str;
This certainly works for the above string, although there maybe some edge cases that I haven't thought of for which this will not work. Whether this will suit your purposes depends on how static the format of the string and the elements/values it contains will be.
这当然适用于上面的字符串,尽管可能有一些我没有想到的边缘情况,这将不起作用。这是否适合您的目的取决于字符串的格式及其包含的元素/值的静态程度。
回答by Brian Layman
TL;DR: Missing quotes is how Chrome shows it is a JSON object instead of a string. Ensure that you have Header('Content-Type: application/json; charset=UTF8'); in PHP's AJAX response to solve the real problem.
TL;DR:缺少引号是 Chrome 显示它是 JSON 对象而不是字符串的方式。确保您有 Header('Content-Type: application/json; charset=UTF8'); 在 PHP 的 AJAX 响应中解决了真正的问题。
DETAILS: A common reason for wanting to solve this problem is due to finding this difference while debugging the processing of returned AJAX data.
细节:想要解决这个问题的一个常见原因是因为在调试返回的 AJAX 数据的处理时发现了这个差异。
In my case I saw the difference using Chrome's debugging tools. When connected to the legacy system, upon success, Chrome showed that there were no quotes shown around keys in the response according to the debugger. This allowed the object to be immediately treated as an object without using a JSON.parse() call. Debugging my new AJAX destination, there were quotes shown in the response and variable was a string and not an object.
I finally realized the true issue when I tested the AJAX response externally saw the legacy system actually DID have quotes around the keys. This was not what the Chrome dev tools showed.
The only difference was that on the legacy system there was a header specifying the content type. I added this to the new (WordPress) system and the calls were now fully compatible with the original script and the success function could handle the response as an object without any parsing required. Now I can switch between the legacy and new system without any changes except the destination URL.
就我而言,我使用 Chrome 的调试工具看到了不同之处。当连接到旧系统时,成功后,Chrome 显示根据调试器,响应中的键周围没有显示引号。这允许在不使用 JSON.parse() 调用的情况下立即将对象视为对象。调试我的新 AJAX 目标时,响应中显示了引号,并且变量是字符串而不是对象。
当我从外部测试 AJAX 响应时,我终于意识到了真正的问题,看到遗留系统实际上 DID 在键周围有引号。这不是 Chrome 开发工具显示的内容。唯一的区别是在遗留系统上有一个指定内容类型的标头。我将它添加到新的 (WordPress) 系统中,现在调用与原始脚本完全兼容,并且成功函数可以将响应作为对象处理,无需任何解析。现在我可以在旧系统和新系统之间切换,除了目标 URL 之外没有任何更改。