EDIT: I've gotten the "famous question" badge with this question, so I figured I'd come back to it and stick what happened to me right at the very tippy top for people searching it to get an answer right away.
编辑:我用这个问题得到了“著名问题”的徽章,所以我想我应该回到它上面,把发生在我身上的事情贴在最整洁的顶部,让人们立即搜索它以得到答案。
Basically, I was new to JSON. JSON is an object (obviously), as it contains all kinds of stuff! So I was like "Hey, javascript, just pop up an alert with all of this JSON data", expecting it to give me the JSON data as a string. But javascript doesn't do that (which is good!), so it was like "Hey, this is how we display objects, [object Object]".
基本上,我是一个新的JSON。JSON是一个对象(显然),因为它包含各种东西!于是我就想:“嘿,javascript,弹出一个包含所有这些JSON数据的警告”,希望它将JSON数据作为字符串提供给我。但是javascript没有这么做(这很好!)
What I could've done is something like alert(obj.DATA[0][1])
and it would've shown me that bit of the object.
我可以做的是像alert(object. data[0][1])这样的东西,它会显示对象的那一部分。
What I really wanted was to verify that I was making good JSON data, which I could've checked with JSON.stringify
.
我真正想要的是验证我正在创建良好的JSON数据,我可以用JSON.stringify检查这些数据。
Anyway, back to our regularly scheduled questions!
不管怎样,回到我们经常计划的问题吧!
I'm trying to get some JSON data with an ajax call, but jQuery doesn't seem to like my JSON.
我试图用ajax调用获取一些JSON数据,但jQuery似乎不喜欢我的JSON。
if I do something like:
如果我做的是:
function init2() {
alert("inside init2");
jQuery.ajax({
url: "/Mobile_ReportingChain.cfm",
type: "POST",
async: false,
success: function (data) {
alert(data);
var obj = jQuery.parseJSON(data);
alert(obj);
}
});
}
I get this as from alert(data):
我从alert(数据)中得到:
{"COLUMNS":["MFIRST_NAME","MLAST_NAME","MMDDL_NAME","MEMPLY_ID","MAIM_NBR","EMPLY_ID"],
"DATA":[
["FNAME1 ","LNAME1 ","MI1 ","000-14-7189","026-0010","000-62-7276"]
,["FNAME2 ","LNAME2 ","MI2 ","000-01-2302","101-1850","000-14-7189"]
,["FNAME3 ","LNAME3 ","MI3 ","000-91-3619","102-1000","000-01-2302"]
,["FNAME4 ","LNAME4 ","MI4 ","000-25-9687","102-1000","000-91-3619"]
]}
which JSONLint says is valid json. alert(obj) gives me this, however:
JSONLint说的是有效的json。然而,alert(obj)告诉我:
[object Object]
adding dataType: "json"
or "text json"
just makes it report [object Object]
at alert(data)
.
添加数据类型:“json”或“文本json”只会使它在alert(数据)处报告[对象]。
I'd really like to get this figured out, does anyone know why it's doing this? I'm pretty new at jQuery, my goal is to get an array for each of the columns. The same code I'm using has worked on a different page it looks like, which is what's bothering me the most.
我真的很想弄清楚,有人知道为什么会这样吗?我是jQuery新手,我的目标是为每个列获取一个数组。我使用的代码在另一个页面上运行,这是我最困扰的地方。
7 个解决方案
#1
32
The alert()
function can only display a string of text. As its only parameter it takes a string or an object. The object will however be converted into a string that can be displayed.
函数的作用是:只能显示一串文本。作为唯一的参数,它接受一个字符串或一个对象。但是,该对象将被转换为可以显示的字符串。
When fetching JSON through jQuery, the $.ajax()
method will automatically parse the JSON and turn it into a JavaScript object for you. Your data
variable is therefor a JavaScript object, and not a JSON string as one might expect.
当通过jQuery获取JSON时,$.ajax()方法将自动解析JSON并将其转换为JavaScript对象。数据变量用于JavaScript对象,而不是JSON字符串。
Since alert()
only can display strings, when trying to alert your data
object, your object will be turned into its string representation. The string representation of a JavaScript object is [object Object]
.
因为alert()只能显示字符串,当试图警告数据对象时,对象将被转换为字符串表示形式。JavaScript对象的字符串表示形式是[对象对象]。
For debug-purposes you can use console.log(data)
instead. You can then inspect the object and its content through the console in your browsers developer tools.
对于调试目的,您可以使用console.log(数据)。然后,您可以通过浏览器开发工具中的控制台检查对象及其内容。
function init2() {
jQuery.ajax({
url: "/Mobile_ReportingChain.cfm",
type: "POST",
dataType: "json",
async: false,
success: function (data) {
console.log(data);
}
});
}
If you for some reason still want to alert the JSON-data, then you would have to turn your data
object back into a JSON-string. To do that you can make use of JSON.stringify
:
如果出于某种原因,您仍然想要通知JSON-data,那么您必须将数据对象重新转换为JSON-string。要做到这一点,你可以使用JSON.stringify:
alert(JSON.stringify(data));
#2
11
it wants a string
它想要一个字符串
var obj = $.parseJSON(JSON.stringify(data));
#3
4
try sending that object to console.log
. You'll get a clearer picture what does it contain.
尝试将该对象发送到console.log。你会更清楚地知道它包含什么。
Also, put dataType: 'json'
and remove parseJSON
because it's all the same.
另外,放置dataType: 'json'并删除parseJSON,因为它是相同的。
#4
2
This is how it's supposed to work. Your JSON becomes a javascript object. You can then manipulate that object as a regular javascript object.
这就是它的工作原理。JSON成为一个javascript对象。然后可以将该对象作为常规的javascript对象进行操作。
data.COLUMNS
for instance should return an array.
数据。实例的列应该返回一个数组。
#5
2
[object Object]
is the string representation of a javascript object.
[对象对象]是javascript对象的字符串表示形式。
Try accessing properties of the object.
尝试访问对象的属性。
alert(data.COLUMNS[0]);
#6
2
jQuery.parseJSON
will convert the json string into json object so alert(obj) will show you [object Object]
since it is an object.
jQuery。parseJSON将json字符串转换为json对象,因此警告(obj)将显示[对象对象],因为它是一个对象。
If you want to see what obj
contains then use console.log(obj)
and then check console log message.
如果您想查看obj包含什么,那么使用console.log(obj),然后检查控制台日志消息。
#7
0
$.getJSON( "UI/entidades.json.php", function(data){
result = JSON.stringify(data);
alert(result)
console.log(result)
})
#1
32
The alert()
function can only display a string of text. As its only parameter it takes a string or an object. The object will however be converted into a string that can be displayed.
函数的作用是:只能显示一串文本。作为唯一的参数,它接受一个字符串或一个对象。但是,该对象将被转换为可以显示的字符串。
When fetching JSON through jQuery, the $.ajax()
method will automatically parse the JSON and turn it into a JavaScript object for you. Your data
variable is therefor a JavaScript object, and not a JSON string as one might expect.
当通过jQuery获取JSON时,$.ajax()方法将自动解析JSON并将其转换为JavaScript对象。数据变量用于JavaScript对象,而不是JSON字符串。
Since alert()
only can display strings, when trying to alert your data
object, your object will be turned into its string representation. The string representation of a JavaScript object is [object Object]
.
因为alert()只能显示字符串,当试图警告数据对象时,对象将被转换为字符串表示形式。JavaScript对象的字符串表示形式是[对象对象]。
For debug-purposes you can use console.log(data)
instead. You can then inspect the object and its content through the console in your browsers developer tools.
对于调试目的,您可以使用console.log(数据)。然后,您可以通过浏览器开发工具中的控制台检查对象及其内容。
function init2() {
jQuery.ajax({
url: "/Mobile_ReportingChain.cfm",
type: "POST",
dataType: "json",
async: false,
success: function (data) {
console.log(data);
}
});
}
If you for some reason still want to alert the JSON-data, then you would have to turn your data
object back into a JSON-string. To do that you can make use of JSON.stringify
:
如果出于某种原因,您仍然想要通知JSON-data,那么您必须将数据对象重新转换为JSON-string。要做到这一点,你可以使用JSON.stringify:
alert(JSON.stringify(data));
#2
11
it wants a string
它想要一个字符串
var obj = $.parseJSON(JSON.stringify(data));
#3
4
try sending that object to console.log
. You'll get a clearer picture what does it contain.
尝试将该对象发送到console.log。你会更清楚地知道它包含什么。
Also, put dataType: 'json'
and remove parseJSON
because it's all the same.
另外,放置dataType: 'json'并删除parseJSON,因为它是相同的。
#4
2
This is how it's supposed to work. Your JSON becomes a javascript object. You can then manipulate that object as a regular javascript object.
这就是它的工作原理。JSON成为一个javascript对象。然后可以将该对象作为常规的javascript对象进行操作。
data.COLUMNS
for instance should return an array.
数据。实例的列应该返回一个数组。
#5
2
[object Object]
is the string representation of a javascript object.
[对象对象]是javascript对象的字符串表示形式。
Try accessing properties of the object.
尝试访问对象的属性。
alert(data.COLUMNS[0]);
#6
2
jQuery.parseJSON
will convert the json string into json object so alert(obj) will show you [object Object]
since it is an object.
jQuery。parseJSON将json字符串转换为json对象,因此警告(obj)将显示[对象对象],因为它是一个对象。
If you want to see what obj
contains then use console.log(obj)
and then check console log message.
如果您想查看obj包含什么,那么使用console.log(obj),然后检查控制台日志消息。
#7
0
$.getJSON( "UI/entidades.json.php", function(data){
result = JSON.stringify(data);
alert(result)
console.log(result)
})