jQuery.getJSON 和 jQuery.parseJSON 返回 [object Object]?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9218900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery.getJSON and jQuery.parseJSON return [object Object]?
提问by Rob
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 不会那样做(这很好!),所以就像“嘿,这就是我们显示对象的方式,[object Object]”。
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(obj.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):
我从警报(数据)中得到了这个:
{"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)
.
添加dataType: "json"
或"text json"
只是让它[object Object]
在alert(data)
.
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 的新手,我的目标是为每一列获取一个数组。我正在使用的相同代码在看起来像的不同页面上工作,这是最困扰我的地方。
回答by Christofer Eliasson
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.
该alert()
函数只能显示一串文本。作为它的唯一参数,它需要一个字符串或一个对象。但是,该对象将转换为可以显示的字符串。
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 对象。data
因此,您的变量是一个 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()
只能显示字符串,因此在尝试警告您的data
对象时,您的对象将转换为其字符串表示形式。JavaScript 对象的字符串表示形式是[object Object]
.
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(data)
改用。然后,您可以通过浏览器开发人员工具中的控制台检查对象及其内容。
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 字符串。为此,您可以使用JSON.stringify
:
alert(JSON.stringify(data));
回答by Harry Forbess
it wants a string
它想要一个字符串
var obj = $.parseJSON(JSON.stringify(data));
回答by Jovan Perovic
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
因为它都是一样的。
回答by ShankarSangoli
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 对象,因此 alert(obj) 会显示给您,[object Object]
因为它是一个对象。
If you want to see what obj
contains then use console.log(obj)
and then check console log message.
如果您想查看obj
包含的内容,请 使用console.log(obj)
并检查控制台日志消息。
回答by Kevin B
[object Object]
is the string representation of a javascript object.
[object Object]
是 javascript 对象的字符串表示形式。
Try accessing properties of the object.
尝试访问对象的属性。
alert(data.COLUMNS[0]);
回答by James Montagne
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.
data.COLUMNS
例如应该返回一个数组。
回答by Vladimir Salguero
$.getJSON( "UI/entidades.json.php", function(data){
result = JSON.stringify(data);
alert(result)
console.log(result)
})