javascript getJSON 只返回 [object Object],[object Object]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11917280/
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
getJSON only returning [object Object],[object Object]
提问by Satch3000
I am testing out some code and I have created a json file with the data.
我正在测试一些代码,并创建了一个包含数据的 json 文件。
The problem is that I'm getting "[object Object],[object Object]" in the alert. No data.
问题是我在警报中收到“[object Object],[object Object]”。没有数据。
What I'm I doing wrong?
我做错了什么?
Here is the code:
这是代码:
<!DOCTYPE HTML>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$.getJSON("appData.json",function(results){alert(results);});
});
</script>
</head>
<body>
</body>
</html>
and here is the content of appData.json
这是 appData.json 的内容
[{"foo":"bar"},{"foo2":"blablabla"}]
Also, The index.html file and the json file are both on my desktop and I am running it from there.
此外,index.html 文件和 json 文件都在我的桌面上,我正在从那里运行它。
回答by Pradeeshnarayan
please try like this:
请尝试这样:
$.getJSON("appData.json", function(results) {
$.each(results, function(index) {
alert(results[index].foo);
});
});
回答by Satch3000
Well, you're getting an array of objects, and arrays and objects are data.
好吧,你得到了一个对象数组,数组和对象都是数据。
// v----first Object in the outer Array
alert(results[0].foo);
// ^----foo property of the first Object
It's just that an alert
shows the default toString()
values of the objects.
只是alert
显示toString()
对象的默认值。
When you use $.getJSON
, jQuery parsed the JSON text into JavaScript objects automatically. If you wanted the raw JSON, then make a $.get
request instead.
当您使用 时$.getJSON
,jQuery 会自动将 JSON 文本解析为 JavaScript 对象。如果您想要原始 JSON,$.get
请改为发出请求。
If you want to iterate the Array, use a for
loop, or one of the iteration methods from jQuery or the native API.
如果要迭代 Array,请使用for
循环,或者来自 jQuery 或本机 API 的迭代方法之一。
回答by adeneo
When alerting an object, it will say just that, [Object]
, and if using Firefox you could always do alert(results.toSource());
but a better option would be to start using the console (F12) instead:
当提醒一个对象时,它会说[Object]
,,如果使用 Firefox,你总是可以这样做,alert(results.toSource());
但更好的选择是开始使用控制台 (F12):
$(document).ready(function() {
$.getJSON("appData.json",function(results){
console.log(results);
});
});
and you can see the entire object and it's structure.
你可以看到整个物体和它的结构。
回答by Travis J
There is no problem there. That is letting you know there are two objects inside of the array. The reason that you are getting that is because it is the top level so you only get types back. A good way to access JSON data is through recursion.
那里没有问题。这让您知道数组中有两个对象。你得到它的原因是因为它是顶级的,所以你只能得到类型。访问 JSON 数据的一种好方法是通过递归。
function ContainsKeyValue( obj, key, value ){
for( all in obj )
{
if( obj[all] != null && obj[all][key] == value ){
return true;
}
if( typeof obj[all] == "object" && obj[all]!= null ){
var found = ContainsKeyValue( obj[all], key, value );
if( found == true ) return true;
}
}
return false;
}
This will start from a given object inside of the graph, and recurse down any objects found. I use it like this:
这将从图中的给定对象开始,并递归找到找到的任何对象。我像这样使用它:
var liveData = [];
for( var item in json.Items)
{
if( ContainsKeyValue( json.Items[item], "Key", "Value" ) === true )
{
liveData.push( json.Items[item] );
}
}