无效的 JSON 字符串谷歌可视化图表

Invalid Json string Google Visualization Chart

本文关键字:可视化 谷歌 字符串 JSON 无效      更新时间:2023-09-26

下面是我的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,但是谷歌js给出了错误。

三件事:

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

其次,我不知道你为什么要在字符串中转义空格、句点和大括号。我无法想象这会如何帮助或伤害你。

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


[编辑]

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

此外,如果您使用任何不安全的内容(例如,用户生成的内容或来自您控制范围之外的任何内容(,则应确保所有内容都经过JSON.parse()。否则,您将面临 XSS 攻击。例如:

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