Json中的解析函数 [英] Parse function inside Json

查看:52
本文介绍了Json中的解析函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于函数JSON.stringify(),我有这个JSON:

I have this JSON as a result of the function JSON.stringify():

{"get":"function (f,g){'use strict';var h,i;if(i={},'string'==typeof f){if('object'==typeof g)for(h in g)i[h]=g[h];i.url=f}else if('object'==typeof f)for(h in f)i[h]=f[h];return i.type=i.type||d,i.oauthio={provider:a,tokens:b,request:c},e.http(i)}","post":"function (f,g){'use strict';var h,i;if(i={},'string'==typeof f){if('object'==typeof g)for(h in g)i[h]=g[h];i.url=f}else if('object'==typeof f)for(h in f)i[h]=f[h];return i.type=i.type||d,i.oauthio={provider:a,tokens:b,request:c},e.http(i)}"}

你怎么能看到那里是值内的函数。我想重建这个JavaScript对象,我的目标是删除risult中的引号以及值;因为在这种情况下,函数被识别为字符串。我想要这样的东西: {key:value}

How you can see there are functions inside values. I want to rebuild this JavaScript object, my goal is to remove the quotes in the risult but also the values; because functions, in this case, are recognized as string. I want something like this:{key : value}

现在我得到: { key:value}

推荐答案

快速回答:

以下函数将执行此操作:

The function bellow will do it:

function fix(obj){

    for (var property in obj) {
        if (obj.hasOwnProperty(property)) {
            obj[property] = eval("(" + obj[property] + ")");
        }
    }

}

如果 obj 有你的JSON解析对象,然后执行以下操作:

If obj has your JSON parsed object then just do the following:

fix(obj);
console.log(obj); // in case you want to see the change in the console

说明(如果您需要) ONE):

如果在调用eval之前用括号'()'括起字符串,你将能够获得javascript函数表达式。

You will be able to get the javascript function expression if you enclose the string with parentheses '()' before invoking eval.

所以实现所需结果的步骤是:

So the steps to achieve the desired result are:


  1. 将函数表达式字符串括起来括号(参见脚注原因)

  2. 调用eval函数来计算函数声明表达式

  3. 将函数声明表达式赋值给同一属性包含字符串值

对于您给出的简单示例,您可以通过以下方式获得所需的结果:

For the simplistic example you gave, you can get the desired results by:

var obj = {"get":"function (f,g){'use strict';var h,i;if(i={},'string'==typeof f){if('object'==typeof g)for(h in g)i[h]=g

[h];i.url=f}else if('object'==typeof f)for(h in f)i[h]=f[h];return i.type=i.type||d,i.oauthio=

{provider:a,tokens:b,request:c},e.http(i)}","post":"function (f,g){'use strict';var h,i;if(i={},'string'==typeof f){if

('object'==typeof g)for(h in g)i[h]=g[h];i.url=f}else if('object'==typeof f)for(h in f)i[h]=f[h];return i.type=i.type||

d,i.oauthio={provider:a,tokens:b,request:c},e.http(i)}"};

obj.get = eval("(" + obj.get + ")");
obj.post = eval("(" + obj.post + ")");

您可以使用以下功能自动执行此操作:

You can automate that by using the following function:

function fix(obj){

    for (var property in obj) {
        if (obj.hasOwnProperty(property)) {
            obj[property] = eval("(" + obj[property] + ")");
        }
    }

}

你的最终代码应该是这样的:

Your final code should be something like:

function fix(obj){

        for (var property in obj) {
            if (obj.hasOwnProperty(property)) {
                obj[property] = eval("(" + obj[property] + ")");
            }
        }

    }


var obj = {"get":"function (f,g){'use strict';var h,i;if(i={},'string'==typeof f){if('object'==typeof g)for(h in g)i[h]=g

    [h];i.url=f}else if('object'==typeof f)for(h in f)i[h]=f[h];return i.type=i.type||d,i.oauthio=

    {provider:a,tokens:b,request:c},e.http(i)}","post":"function (f,g){'use strict';var h,i;if(i={},'string'==typeof f){if

    ('object'==typeof g)for(h in g)i[h]=g[h];i.url=f}else if('object'==typeof f)for(h in f)i[h]=f[h];return i.type=i.type||

    d,i.oauthio={provider:a,tokens:b,request:c},e.http(i)}"};

fix(obj);






脚注:


Footnotes:

如果您有兴趣知道为什么需要括号,请检查以下链接:

In case you have interest to know why the parentheses are needed please check the link below:

为什么JavaScript的eval需要括号来评估JSON数据?

这篇关于Json中的解析函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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