无效的Json字符串Google Visualization Chart

时间:2016-02-04 07:50:16

标签: javascript c# json charts google-visualization

以下是我的JSON字符串

{
"cols": [{
    "type": "string",
    "id": "Date",
    "label": "Date"
}, {
    "type": "string",
    "id": "Tweet",
    "label": "Tweet"
}, {
    "type": "number",
    "id": "Retweets",
    "label": "Retweets"
}, {
    "type": "number",
    "id": "Favorites",
    "label": "Favorites"
}],
"rows": [{
    "c": [{
        "v": "<a href='' target='_blank'>14 Jan 2016 10:36PM</a>"
    }, {
        "v": "@sammaanchhabra\ We\ would\ like\ to\ assure\ u\ that\ we\ haven''t\ changed\ anything\ related\ to\ weight\.\(1/2\)"
    }, {
        "v": 0
    }, {
        "v": 1
    }]
}, {
    "c": [{
        "v": "<a href='' target='_blank'>14 Jan 2016 10:35PM</a>"
    }, {
        "v": "@sammaanchhabra\ We\ have\ kept\ these\ entities\ same\ as\ they\ were\ when\ we\ exited\ the\ market\.\ \(2/2\)"
    }, {
        "v": 0
    }, {
        "v": 0
    }]
}, {
    "c": [{
        "v": "<a href='' target='_blank'>14 Jan 2016 11:56AM</a>"
    }, {
        "v": "@RishutaKarthikD\ You\ can\ surely\ cook\ MAGGI\ Noodles\ in\ 2\ minutes\ by\ following\ the\ suggested\ method\ of\ preparation,\ as\ mentioned\ on\ pack\."
    }, {
        "v": 0
    }, {
        "v": 0
    }]
}, {
    "c": [{
        "v": "<a href='' target='_blank'>13 Jan 2016 07:39PM</a>"
    }, {
        "v": "@BTofficiel\ It''s\ good\ to\ be\ back!\ Enjoy\ your\ MAGGI\ Noodles\ :\)"
    }, {
        "v": 1
    }, {
        "v": 0
    }]
}, {
    "c": [{
        "v": "<a href='' target='_blank'>13 Jan 2016 07:05PM</a>"
    }, {
        "v": "@_clue_less\ we\ missed\ you\ too\ :\)\ Thank\ you\ for\ all\ the\ love\ and\ support\."
    }, {
        "v": 0
    }, {
        "v": 1
    }]
}, {
    "c": [{
        "v": "<a href='' target='_blank'>13 Jan 2016 01:08PM</a>"
    }, {
        "v": "@AakashRoyDC\ Thanks\ for\ your\ love\ !\ Delighted\ to\ have\ fans\ like\ you\."
    }, {
        "v": 0
    }, {
        "v": 1
    }]
}, {
    "c": [{
        "v": "<a href='' target='_blank'>13 Jan 2016 11:55AM</a>"
    }, {
        "v": "@AninBanerjeeeee\ \ In\ the\ initial\ phase\ we\ are\ rolling\ out\ MAGGI\ Noodles\ Masala,\ the\ most\ popular\ variant\ amongst\ our\ consumers\.\ \(1/2\)"
    }, {
        "v": 0
    }, {
        "v": 0
    }]
}, {
    "c": [{
        "v": "<a href='' target='_blank'>13 Jan 2016 11:54AM</a>"
    }, {
        "v": "@AninBanerjeeeee\ \ \ We\ will\ roll\ out\ some\ of\ our\ other\ variants\ as\ soon\ as\ possible\.\ \(2/2\)"
    }, {
        "v": 0
    }, {
        "v": 0
    }]
}, {
    "c": [{
        "v": "<a href='' target='_blank'>13 Jan 2016 11:18AM</a>"
    }, {
        "v": "@shrutideb\ \ We\ are\ concerned\ and\ would\ like\ to\ talk\ to\ you\ &amp;\ investigate\.\ Please\ DM\ your\ contact\ and\ location\ details\."
    }, {
        "v": 0
    }, {
        "v": 0
    }]
}, {
    "c": [{
        "v": "<a href='' target='_blank'>12 Jan 2016 11:09PM</a>"
    }, {
        "v": "@Chethan14802058\ We\ are\ in\ touch\ with\ our\ channel\ partners\ and\ distributors\.\ \nThey\ are\ enthusiastic\ about\ the\ re-introduction\ of\ MAGGI\ \(1/2\)"
    }, {
        "v": 0
    }, {
        "v": 1
    }]
}]
}

当我将变量strJSON中的JSON传递给

var data = new google.visualization.DataTable(strJSON);

它给了我错误JavaScript runtime error: Invalid JSON string:。我已经在jsonlint上验证了上面的JSON,但google js给出了错误。

1 个答案:

答案 0 :(得分:1)

三件事:

首先,如果您使用单引号将此作为字符串文字包含在您的JSON代码中(也就是说,您不是通过AJAX加载它,而是作为脚本中的实际字符串块),您应该确保您正在转义所有换行符(将其替换为\n)和所有单引号(将其替换为\')。字符串中的换行符必须进行转义,因为JS将换行符解释为语句的结尾,这意味着您将打开一个字符串文字而不关闭它。如果你的字符串包含与包装它相同的引号字符,则需要对其进行转义,因为这会过早地关闭字符串文字。

其次,我不知道你为什么要逃避字符串中的空格,句号和括号。我无法想象这会对你有什么帮助或伤害。

第三,看起来你正在以SQL的风格转义单引号。这可能是有原因的,但同样,我无法想象它会如何帮助你(如果你认为它会保护你免受SQL注入或类似的事情,那么你可能有更深层次的问题)。

[编辑]

根据您在下面的评论,我建议您使用Microsoft为JavaScript serialisation提供的工具。

此外,如果您使用任何不安全的内容(例如,用户生成的内容或源自您控件之外的任何内容),您应该确保所有内容都通过JSON.parse()。否则,你会对XSS攻击持开放态度。例如:

var objJSON = JSON.parse(strJSON),
    data = new google.visualization.DataTable(objJSON);