JavaScript JSON 对象对象错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28650150/
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
JavaScript JSON Object Object error
提问by user2219097
I am trying to print out the key and value names from the data below e.g. "Player 1' - 'ball' 'hat' and "Player 2 - 'ball, 'hat'. So keys have multiple value names but im not sure how i print these out.** I am getting '[object Object]' as a response**. Can someone help me with this understanding please and trying to resolve the matter.
我试图从下面的数据中打印出键和值名称,例如“玩家 1” - '球' '帽子' 和“玩家 2 - '球,'帽子'。所以键有多个值名称,但我不知道我如何打印这些。** 我得到 '[object Object]' 作为响应**。有人可以帮助我理解这种理解并试图解决这个问题。
Data
数据
{
"Player1": {
"ball": 1,
"hat": 2
},
"Player2": {
"ball": 1,
"hat": 2,
}
}
JavaScript
JavaScript
$.getJSON('data.json', function (data) {
var response = data;
for (key in response){
alert("key: " + key + "value :" + response[key]);
}
});
回答by Nick Craver
The simplest way to do this in any modern browserwould be to use Object.keys()
and just join the result into a string, like this:
在任何现代浏览器中执行此操作的最简单方法是使用Object.keys()
并将结果连接到一个字符串中,如下所示:
for (key in response){
alert("key: " + key + " value :" + Object.keys(response[key]).join(' '));
}
Result:
结果:
key: Player1 value :ball hat
key: Player2 value :ball hat
键:
Player1 值:球帽键:Player2 值:球帽
回答by Jimmy Bernljung
Use JSON.stringify(response[key])
when printing the object.
JSON.stringify(response[key])
打印对象时使用。
回答by void
$.getJSON('data.json', function (data) {
var response = data;
for (key in response){
alert("key: " + key + "value :" + JSON.stringify(response[key]));
}
});
or use response[key].ball, response[key].hat
或使用 response[key].ball, response[key].hat
回答by arnav
The stringify
function of JSON
comes to rescue here. Since most internal functions take data parameters as string or sometimes buffer.
Thus you can use the following code:-
的stringify
功能JSON
来拯救这里。由于大多数内部函数将数据参数作为字符串或有时是缓冲区。因此,您可以使用以下代码:-
var response = {
"Player1": {
"ball": 1,
"hat": 2
},
"Player2": {
"ball": 1,
"hat": 2,
}
}
var dataString = JSON.stringify(response);
Now use dataString
for sending and receiving over different calls.
现在dataString
用于通过不同的呼叫发送和接收。
回答by jyrkim
Here's one way of printing the content of the object. Object.keys()method is used to access player's "items" and get those in array format. Updated.
这是打印对象内容的一种方法。Object.keys()方法用于访问玩家的“物品”并以数组格式获取它们。更新。
var response = {
"Player1": {
"ball": 1,
"hat": 2
},
"Player2": {
"ball": 1,
"hat": 2,
}
};
for (player in response) {
var items = Object.keys(response[player]);
var itemText = "";
for (i = 0; i < items.length; i++) {
itemText += " '" + items[i] + "'";
}
console.log(player + " -" + itemText);
//alternative way, suggested by NickCraver
console.log(player + " - "+ Object.keys(response[player]).map(function(k) { return '\''+k+'\''; }).join(' ') );
}