【问题标题】:Invalid Json string Google Visualization Chart无效的 Json 字符串 Google 可视化图表
【发布时间】:2016-02-04 07:50:15
【问题描述】:

下面是我的 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 给出了错误。

【问题讨论】:

    标签: javascript c# json charts google-visualization


    【解决方案1】:

    三件事:

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

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

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


    [编辑]

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

    此外,如果您使用任何不安全的内容(例如,用户生成的内容或源自您无法控制的任何内容),您应该确保一切都通过JSON.parse()。否则,你会让自己容易受到 XSS 攻击。例如:

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

    【讨论】:

    • 1) 你能帮我解决javascript中换行符的替换代码吗? 2) 我在服务器端从我的 C# 代码传递这个 JSON 并避免任何转义字符,我正在编写 Regex.Escape("String") 来转义所有这些东西。 3)这又是因为我的 C# 转义
    猜你喜欢
    • 2011-08-25
    • 2016-08-09
    • 2015-04-23
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多