正则表达式在javascript中的键周围添加双引号 [英] regular expression to add double quotes around keys in javascript

查看:154
本文介绍了正则表达式在javascript中的键周围添加双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery的getJSON函数来发出请求并处理JSON响应。问题是我回来的反应是不正确的,我无法改变它。响应看起来像这样:

I am using jQuery's getJSON function to make a request and handle the JSON response. The problem is the response I get back is malformed and I can't change it. The response looks something like this:

{
    aNumber: 200,    
    someText: '\'hello\' world',
    anObject: {
        'foo': 'fooValue',
        'bar': '10.0'
    } 
}

要成为有效的JSON,它应如下所示:

To be valid JSON, it should look like this:

{
    "aNumber": 200,    
    "someText": "'hello' world",
    "anObject": {
        "foo": "fooValue",
        "bar": "10.0"
    } 
}

我想将返回的文本更改为有效的JSON对象。我已经使用javascript替换函数将单引号转换为双引号并将转义的单引号转换为单引号,但现在我仍然坚持找出在键值附近添加引号的最佳方法。例如,我如何将foo:fooValue更改为foo:fooValue?是否有正则表达式可以使这很容易?

I would like to change the text returned to a valid JSON object. I've used the javascript replace function to turn the single quotes into double quotes and the escaped single quotes into single quotes, but now I am stuck on figuring out the best way to add quotes around the key values. For example, how would I change foo: "fooValue" to "foo":"fooValue"? Is there a regular expression that can make this easy?

提前致谢!

推荐答案

编辑—回过头来指出,首先,这不是一个可以用正则表达式解决的问题。

edit — came back to point out, first and foremost, that this is not a problem that can be solved with a regular expression.

将JSON表示法区分为序列化是很重要的表单和JavaScript对象常量表示法。

It's important to distinguish between JSON notation as a serialized form, and JavaScript object constant notation.

这:

{ x: "hello" }

是一个完全有效的JavaScript值(表达式片段),所以这个:

is a perfectly valid JavaScript value (an expression fragment), so that this:

var y = { x: "hello" };

给出与以下完全相同的结果:

gives you exactly the same result as:

var y = { "x": "hello" };

换句话说,任何一种情况下y的值都是完全相同的。完全,完全相同,这样就不可能分辨出这两个常数中的哪一个用于初始化y。

In other words, the value of "y" in either of those cases will be exactly the same. Completely, exactly the same, such that it would not be possible to ever tell which of those two constants was used to initialize "y".

现在,如果你想要什么要做的是将包含没有引号的JavaScript样式JSON速记的字符串转换为有效的JSON,唯一要做的就是解析它并使用属性名称周围的引号重新构造字符串。也就是说,你必须编写自己的宽松JSON解析器,而不是处理不带引号的标识符作为属性名称,否则找到一个可以处理这种放松语法的现成解析器。

Now, if what you want to do is translate a string containing JavaScript style "JSON shorthand" without quotes into valid JSON, the only thing to do is parse it and reconstruct the string with quotes around the property names. That is, you will have to either write your own "relaxed" JSON parser than can cope with unquoted identifiers as property names, or else find an off-the-shelf parser that can handle such relaxed syntax.

在你的情况下,看起来一旦你有放松的解析器,你就完成了;你不应该需要翻译。值得庆幸的是,您的无效JSON响应完全可由JavaScript本身解释,因此,如果您信任数据源(并且这是if),您应该能够使用eval()来评估它。

In your case, it looks like once you have the "relaxed" parser available, you're done; there shouldn't be any need for you to translate back. Thankfully, your "invalid" JSON response is completely interpretable by JavaScript itself, so if you trust the data source (and that's a big "if") you should be able to evaluate it with "eval()".

这篇关于正则表达式在javascript中的键周围添加双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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