从键上的json_encoded字符串中删除双引号 [英] Remove double-quotes from a json_encoded string on the keys

查看:394
本文介绍了从键上的json_encoded字符串中删除双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个json_encoded数组,这很好。

I have a json_encoded array which is fine.

我需要在从函数返回的json字符串的所有键上去掉双引号打电话。

I need to strip the double-quotes on all of the keys of the json string on returning it from a function call.

我如何做到这一点并成功归还?

How would I go about doing this and returning it successfully?

谢谢!

我道歉,这里是json代码的片段:

I do apologise, here is a snippet of the json code:

{"start_date":"2011-01-01 09:00","end_date":"2011-01-01 10:00","text":"test"}

添加更多信息:

我将通过AJAX请求检索JSON,所以,如果它更容易,我愿意在javascript方面如何做到这一点。

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.

推荐答案

已编辑根据anubhava的评论

EDITED as per anubhava's comment

$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.

这篇关于从键上的json_encoded字符串中删除双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆