Remove double-quotes from a json_encoded string on the keys

时间:2022-09-15 12:58:00

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方面如何做到这一点的想法。

1 个解决方案

#1


7  

EDITED as 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*/', '$1:', $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.

这肯定适用于上面的字符串,虽然可能有一些我没有想到的边缘情况,这不起作用。这是否适合您的目的取决于字符串的格式和它包含的元素/值的静态程度。

#1


7  

EDITED as 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*/', '$1:', $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.

这肯定适用于上面的字符串,虽然可能有一些我没有想到的边缘情况,这不起作用。这是否适合您的目的取决于字符串的格式和它包含的元素/值的静态程度。