jQuery 从 json 对象获取键名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4652663/
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
get key name from json object
提问by pedalpete
This is almost exactly the same as this question json get key names as text?, but that response isn't working for me.
这与这个问题json get key names as text几乎完全一样?,但这种反应对我不起作用。
I've got a json object
我有一个 json 对象
{"userList":[ {"user1":[{"username":"mike","memberSince":"01/03/2011"}]}, {"user2":[{"username":"john","memberSince":"01/05/2011"}]}, ]}
The only reason I have a the "user1" and "user2" labels is because I'm storing the userlist by userId in jQuery data, and then stringifying it to put it in a cookie and send it in another page. I know, sounds stupid, but I'm just building some front end stuff to prove a point before doing this properly with a db.
我有“user1”和“user2”标签的唯一原因是因为我在 jQuery 数据中按 userId 存储用户列表,然后将其字符串化以将其放入 cookie 并将其发送到另一个页面。我知道,听起来很愚蠢,但我只是在构建一些前端的东西来证明一个观点,然后再用 db 正确地做这件事。
So the
所以
jQuery('div#userList').data('user1',user1JSON);
turns into
变成
{"user1":[{"username":"mike","memberSince":"01/03/2011"}]}
when using
使用时
var userlist=JSON.stringify(jQuery('div#userList').data());
Because of this, the user info is now a child of the userId, instead of just being a child of userlist.
因此,用户信息现在是 userId 的子级,而不仅仅是 userlist 的子级。
In order to get the user info, I should be able to say
为了获取用户信息,我应该可以说
for(u=0;u<userList.length;u++){ var userInfo=userList[u][0]; }
but unfortunately this is just giving me an undefined error. If I use
但不幸的是,这只是给了我一个未定义的错误。如果我使用
var userInfo = userList[u]['user1'];
I get the user info correctly.
我正确获取用户信息。
So, can somebody correct me as to why userList[u][0] does not work, OR
所以,有人可以纠正我为什么 userList[u][0] 不起作用,或者
explain to me how to use JSON.stringify without adding the userid to the beginning of the string?
向我解释如何使用 JSON.stringify 而不将 userid 添加到字符串的开头?
回答by Gabriele Petrioli
That is because the userList[u]
returns an object, not an array.
那是因为userList[u]
返回的是一个对象,而不是一个数组。
And you cannot access the object properties with an index..
并且您无法使用索引访问对象属性..
you could try
你可以试试
for(u=0;u<userList.length;u++){
for (var user in userList[u])
{
// user is the key
// userList[u][user] is the value
var userInfo=userList[u][user];
}
}
回答by afifsay
You can use jQuery each function to iterate.
您可以使用 jQuery 每个函数进行迭代。
data=obj.userList
jQuery.each(data, function(key,value){
var username=value.username;
var memberSince=value.memberSince;
...
}
回答by Dave Ward
How about changing your object like this:
如何像这样改变你的对象:
{"userList":[
{"id": "user1", "username": "mike", "memberSince": "01/03/2011"},
{"id": "user2", "username": "john", "memberSince": "01/05/2011"}
]}
Then, you can index into the userList array more naturally. That's probably more like what your data will eventually look like coming back from a database backend anyway.
然后,您可以更自然地索引到 userList 数组。无论如何,这可能更像是您的数据最终从数据库后端返回的样子。