Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript &引用;意外标记o";使用$.parseJSON时_Javascript_Jquery_Json - Fatal编程技术网

Javascript &引用;意外标记o";使用$.parseJSON时

Javascript &引用;意外标记o";使用$.parseJSON时,javascript,jquery,json,Javascript,Jquery,Json,以下ajax调用给出以下结果: $.ajax({ type: "POST", url: //**My full URL goes here**, data: {sources: sources}, dataType: "json", success: function(data) { alert(data); alert(data.length);

以下ajax调用给出以下结果:

    $.ajax({
        type: "POST",
        url:  //**My full URL goes here**,
        data: {sources: sources},
        dataType: "json",
        success: function(data) {
            alert(data);
            alert(data.length);
            for (var i = 0; i < data.length; i++)
            {
                alert(data[i]);
            }
        }
    });
下面是我刚刚添加的代码:

var data = $.parseJSON(data);
$.ajax({
    type: "POST",
    url:  //**My full URL goes here**,
    data: {sources: sources},
    dataType: "json",
    success: function(data) {
        var data = $.parseJSON(data);
        alert(data);
        alert(data.length);
        for (var i = 0; i < data.length; i++)
        {
            alert(data[i]);
        }
    }
});
var data=$.parseJSON(数据);
$.ajax({
类型:“POST”,
url://**我的完整url位于此处**,
数据:{sources:sources},
数据类型:“json”,
成功:功能(数据){
var data=$.parseJSON(数据);
警报(数据);
警报(数据长度);
对于(变量i=0;i
上面的代码给出了以下错误:

未捕获语法错误:意外的令牌o


为什么呢?我做错什么了吗?如何修复它?

由于您正在传递
数据类型:“json”
,因此
数据已经是已解析的对象,因此无需再次解析它


再次要调试和检查数据值,请使用而不是
alert()
,如
console.log(data)
停止使用
alert
要调试,请改用
console.log

success: function(data) {
  // in the browser console, you will see the data structure.
  // then do what you want
  console.log(data);
  // ...

您已经收到一个json对象。那你为什么还要再次解析呢?第一个代码是正确的,有什么问题吗?jQuery已经为您解析了JSON,现在您有了一个对象数组。您正在将对象转换为字符串,这就是为什么会看到
[object object]
。一切都好。如果您想知道如何正确访问数据:。关于第二个代码中的错误:这是因为
$。parseJSON
需要字符串,而不是数组或对象。谢谢:)在循环中,我想访问对象的内容!我该怎么做?是的,您已经收到解析数据
alert()
不是一个很好的调试工具。谢谢你,阿伦!因此,您能告诉我如何在我的第一个解决方案中访问对象的内容,而不是使用[ObjectObject]可能
数据[0]
或其他东西吗?从您共享的内容来看,
数据
是一个数组。。。你能做一下console.log(data)
看看数据的结构吗。。。您也可以尝试
console.log(JSON.stringify(data))
打印字符串representation@user3000457:这取决于对象具有哪些属性。请看一看。我还建议您阅读@user3000457。您还可以使用浏览器开发人员工具检查实际响应,以查看返回的数据
success: function(data) {
  // in the browser console, you will see the data structure.
  // then do what you want
  console.log(data);
  // ...