无效的Json字符串Google可视化图表 - javascript

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

参考方案

三件事:

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

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

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

[编辑]

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

另外,如果您使用的是任何不安全的内容(例如,用户生成的内容或任何来自控件外部的内容),则应确保所有内容均通过JSON.parse()。否则,您将很容易受到XSS攻击。例如:

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

使用php重新加载内容 - javascript

在对网站进行编程时,我以前使用过此代码,它可以完美工作,但是现在当我想使用一些Flash部件时,每次单击链接时,它都会重新加载所有网站。源代码: <!DOCTYPE html> <html> <head> <title>Hot King Staff</title> <meta charset=…

用jQuery填充模式形式 - javascript

我正在将订单表从数据库绘制到datatables(jquery插件)中。我要在每笔最后一笔交易或每笔交易中增加付款。问题是,如何获取单击添加付款按钮以添加付款的行的订单ID。其次,当点击addpayment时,它会弹出一个带有字段或订单号的模态表单。我想用在td中找到的订单ID填充该字段,并使其不可编辑或隐藏,但在提交模态表单时将其发布到服务器。表格和模式表…

保留文本区域的数据或值,然后选择输入 - javascript

通过$ _POST提交表单时,输入文件值仍然保留。像这样: if($_POST){ $error = false; if(!$post['price']){ $error = true; $error_note['price'] = "*Should not be empty"; } if($err…

带有AJAX和DOM处理API的下拉菜单 - javascript

我从API获取数据,但未在我的下拉菜单中显示。如果我用?act=showprovince回显,结果就在那里。example.html<head> <link rel="stylesheet" type="text/css" href="css/normalize.css"> …

尽管刷新,jQuery格式仍未应用于Ajax数据 - javascript

我正在通过GET响应消息从服务器(php文件)的可折叠内部加载列表视图。但是,尽管刷新了jQuery元素,但jQuery格式并未应用于添加的HTML。我的页面在这里:http://i.cs.hku.hk/~hsbashir/Project_Work/events/events.htmlHTML代码(仅相关代码)<script> lastRecor…