JSON 解析错误 - JSON 中位置 1 的意外标记 o [英] JSON parsing error - Unexpected token o in JSON at position 1
问题描述
我需要获取一个 JSON 对象并为自动完成功能记录标题控制台.我的 json 的示例如下:
<预><代码>[{"title": "示例 1","url": "http:\/\/www.example1.com\/"},{"title": "示例 2","url": "http:\/\/www.example2.com\/"},{"title": "示例 3","url": "http:\/\/www.example3.com\/"}, ...我想像这样在我的控制台中记录所有标题:
示例1示例 2示例 3
我最初尝试做我想做的事情是这样的:
$.ajax({类型:获取",数据类型:'json',url: "/url/to/json/",成功:功能(数据){var searchResults = JSON.parse(data);console.log(searchResults.title);},});
返回:
<块引用>JSON 中位置 1 的意外标记 o
经过进一步研究:
SyntaxError: Unexpected token o in JSON at position 1
是什么导致了未捕获的语法错误:带有 $.parseJSON() 和 JSON.parse()
的意外标记 o"建议数据已经解析完毕.因此,我尝试按照这些答案的建议直接调用该对象:
$.ajax({类型:获取",数据类型:'json',url: "/url/to/json/",成功:功能(数据){控制台日志(数据.标题);},});
这给了我:
<块引用>未定义
如何在控制台中打印特定的 JSON 数据,在这种情况下 - 标题?如果数据已经被解析,那么为什么当我尝试访问它时它返回 undefined?
如果您的数据具有以下格式:
<预><代码>[{"title": "示例 1","url": "http:\/\/www.example1.com\/"},{"title": "示例 2","url": "http:\/\/www.example2.com\/"},...要打印每个title
/url
,您需要遍历结果(使用for
或调用forEach
代码>如下):
$.ajax({类型:获取",数据类型:'json',url: "https://api.myjson.com/bins/1czpnp",成功:功能(数据){控制台日志(数据.标题);//不明确的控制台日志(数据);//物体//要打印标题/网址,请遍历数组//备选方案 1(普通用于):for(var i = 0; i < data.length; i++) {var d = 数据[i];console.log('title: ' + d.title + ' ~ url: ' + d.url);};//备选方案 2 (forEach):data.forEach(函数(d){console.log('title: ' + d.title + ' ~ url: ' + d.url);});},});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
评论问题
<块引用>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); });
将在控制台打印:
123
这是调用function(number){console.log(number);的结果}
三次(myArray
数组的每个元素一次),其中第一个 number
值为 1
,第二个一次是 2
,最后一次是 3
.
为什么vanilla JS循环没有传入(d)
for
只是执行给定语句给定次数.它不会通过 d
因为不涉及 function
参数(就像 .forEach
发生的那样).
换句话说,一个 for
是:
for(var i = 0; i
所以当我们这样做
for(var i = 0; i
我们要求执行一些命令 data.length
次.data.length
是一个数字,表示数据数组的长度.(例如 ['a','b'].length
是 2).
由于它只是执行一个命令,我们每次都必须自己创建"d
,因此:var d = data[i];
来获取每个<代码>数据元素.
I need to take a JSON object and get the titles console logged for an auto complete feature. AN example of what my json looks like is below:
[
{
"title": "Example 1",
"url": "http:\/\/www.example1.com\/"
},
{
"title": "Example 2",
"url": "http:\/\/www.example2.com\/"
},
{
"title": "Example 3",
"url": "http:\/\/www.example3.com\/"
}, ...
I would like to log all the titles in my console like this:
Example 1
Example 2
Example 3
My initial attempt to do what I want was this:
$.ajax({
type: "GET",
dataType: 'json',
url: "/url/to/json/",
success: function(data) {
var searchResults = JSON.parse(data);
console.log(searchResults.title);
},
});
This returned:
Unexpected token o in JSON at position 1
After some further research:
Unexpected token o in JSON at position 1
SyntaxError: Unexpected token o in JSON at position 1
What is causing "Uncaught SyntaxError: Unexpected token o" with $.parseJSON() and JSON.parse()
It is suggested that the data is already parsed. So I try to call the object directly as these answers suggest:
$.ajax({
type: "GET",
dataType: 'json',
url: "/url/to/json/",
success: function(data) {
console.log(data.title);
},
});
This gives me:
undefined
How do I print specific JSON data in console, in this case - title? If data is already parsed then how come when I try to access it it returns undefined?
If your data has the format:
[
{
"title": "Example 1",
"url": "http:\/\/www.example1.com\/"
},
{
"title": "Example 2",
"url": "http:\/\/www.example2.com\/"
},
...
To print each title
/url
, you need to iterate through the result (using a for
or calling forEach
as below):
$.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);
});
},
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Comments questions
console.log(data.title);
is undefined because we are not looping through it, right?
Kind of. It is undefined
because data
is an array. And JavaScript arrays don't have a title
property.
in the line
data.forEach(function (d) {
, what exactly is(d)
? Where is that value coming from? What does it represent?
.forEach
is a method present in JavaScript arrays. It takes a function as argument and then calls that function on each element of the array.
Example:
var myArray = [1, 2, 3];
myArray.forEach(function (number) { console.log(number); });
Will print in the console:
1
2
3
Which is the result of calling function (number) { console.log(number); }
three times (one for each element of the myArray
array), where the first number
value will be 1
, the second time it will be 2
and the last time it will be 3
.
And why does the vanilla JS loop not pass in
(d)
A for
is a mere execution of a given statement a given number of times. It does not pass d
because there is no function
argument involved (as it happens with .forEach
).
In other words, a for
is:
for(var i = 0; i < n; i++) {
// execute whatever is here "n" times, each time having "i" being a different value
}
So when we do
for(var i = 0; i < data.length; i++) {
// some command
}
We are asking to execute some command data.length
times. data.length
is a number that means the length of the data array. (e.g. ['a','b'].length
is 2).
Since it just executes a command, we have to "create" the d
ourselves each time, thus: var d = data[i];
to get each data
element.
这篇关于JSON 解析错误 - JSON 中位置 1 的意外标记 o的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!