JSON解析错误 - 位置1的JSON中出现意外的令牌o

kaw*_*nah 2 javascript jquery json

我需要获取一个JSON对象并记录标题控制台以获取自动完成功能.我的json的样子如下:

[
  {
    "title": "Example 1",
    "url": "http:\/\/www.example1.com\/"
  },
  {
    "title": "Example 2",
    "url": "http:\/\/www.example2.com\/"
  },
  {
    "title": "Example 3",
    "url": "http:\/\/www.example3.com\/"
  }, ...
Run Code Online (Sandbox Code Playgroud)

我想在我的控制台中记录所有标题,如下所示:

Example 1
Example 2
Example 3
Run Code Online (Sandbox Code Playgroud)

我最初尝试做我想要的是这样的:

$.ajax({
      type: "GET",
      dataType: 'json',
      url: "/url/to/json/",
      success: function(data) {
        var searchResults = JSON.parse(data);
        console.log(searchResults.title);
      },
    });
Run Code Online (Sandbox Code Playgroud)

这返回:

在位置1的JSON中出现意外的令牌o

经过进一步的研究:

在位置1的JSON中出现意外的令牌o

SyntaxError:位置1的JSON中出现意外的标记o

使用$ .parseJSON()和JSON.parse()导致"Uncaught SyntaxError:Unexpected token o"的原因是什么

建议已经解析数据.所以我尝试直接调用对象,因为这些答案建议:

$.ajax({
      type: "GET",
      dataType: 'json',
      url: "/url/to/json/",
      success: function(data) {
        console.log(data.title);
      },
    });
Run Code Online (Sandbox Code Playgroud)

这给了我:

未定义

如何在控制台中打印特定的JSON数据,在本例中为标题?如果已经解析了数据,那么当我尝试访问它时,它会返回undefined?

acd*_*ior 5

如果您的数据具有以下格式:

[
  {
    "title": "Example 1",
    "url": "http:\/\/www.example1.com\/"
  },
  {
    "title": "Example 2",
    "url": "http:\/\/www.example2.com\/"
  },
...
Run Code Online (Sandbox Code Playgroud)

要打印每个title/ url,您需要遍历结果(使用for或调用forEach如下):

$.ajax({
  type: "GET",
  dataType: 'json',
  url: "https://api.myjson.com/bins/1czpnp",
  success: function(data) {
    console.log(data.title); // undefined
    console.log(data); // the [{...}] object

    // to print the title/urls, iterate over the array
    // alternative 1 (ordinary for):
    for(var i = 0; i < data.length; i++) {
      var d = data[i];
      console.log('title: ' + d.title + ' ~ url: ' + d.url);
    };

    // alternative 2 (forEach):
    data.forEach(function (d) {
      console.log('title: ' + d.title + ' ~ url: ' + d.url);
    });
  },
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

评论问题

console.log(data.title); 是不确定的,因为我们没有循环,对吧?

的种类.这是undefined因为data是一个数组.JavaScript数组没有title属性.

在线data.forEach(function (d) {,究竟是(d)什么?这个价值来自哪里?它代表什么?

.forEach是JavaScript数组中存在的方法.它接受一个函数作为参数,然后在数组的每个元素上调用该函数.

例:

var myArray = [1, 2, 3];
myArray.forEach(function (number) { console.log(number); });
Run Code Online (Sandbox Code Playgroud)

将在控制台中打印:

1
2
3
Run Code Online (Sandbox Code Playgroud)

这是调用function (number) { console.log(number); }三次的结果(对于myArray数组的每个元素一次),第一个number值将是1第二次,第二次2是最后一次3.

为什么香草JS循环不会传入 (d)

A for只是给定语句执行给定次数.它没有通过,d因为没有function涉及参数(因为它发生.forEach).

换句话说,a for是:

for(var i = 0; i < n; i++) {
  // execute whatever is here "n" times, each time having "i" being a different value
}
Run Code Online (Sandbox Code Playgroud)

所以,当我们这样做

for(var i = 0; i < data.length; i++) {
  // some command
}
Run Code Online (Sandbox Code Playgroud)

我们要求执行一些命令data.length时间.data.length是一个数字,表示数据数组的长度.(例如['a','b'].length是2).

由于它只是执行一个命令,我们d每次都必须"创造" 自己,因此:var d = data[i];获得每个data元素.