jQuery 在javascript中解析json字典以遍历键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9138959/
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
parsing json dictionary in javascript to iterate through keys
提问by almighty
I have the below json data coming from web service which is nothing but a dictionary serialized into json, now on client side i need to parse this back to iterate through the keys of this json dictionary using javascript or jquery
我有以下来自 Web 服务的 json 数据,它只不过是序列化为 json 的字典,现在在客户端,我需要将其解析回以使用 javascript 或 jquery 遍历此 json 字典的键
{
???"AboutToExpire": {
??????"Display": true,
??????"Message": "Several of your subscriptions are about to expire. <a id=\"lnkShowExpiringSubs\" href=\"#\">View subscriptions<\/a>"
???},
???"Expired": {
??????"Display": true,
??????"Message": "Your McAfee WaveSecure - Tablet Edition subscription has expired and you've been without protection for 384 days. <a id=\"lnkNotificationRenewNow\" href=\"http://home.mcafee.com/root/campaign.aspx?cid=96035&pk=FAB37CF4-3680-4A87-A253-77E7D48BF6D7&affid=0\">Renew now<\/a>"
???}
}
采纳答案by craftsman
var s = '{"AboutToExpire":{"Display":true,"Message":"Several of your subscriptions are about to expire. \u003ca id=\"lnkShowExpiringSubs\" href=\"#\"\u003eView subscriptions\u003c/a\u003e"},"Expired":{"Display":true,"Message":"Your McAfee WaveSecure - Tablet Edition subscription has expired and you've been without protection for 384 days. \u003ca id=\"lnkNotificationRenewNow\" href=\"http://home.mcafee.com/root/campaign.aspx?cid=96035&pk=FAB37CF4-3680-4A87-A253-77E7D48BF6D7&affid=0\"\u003eRenew now\u003c/a\u003e"}}';
var data = eval(s); // this will convert your json string to a javascript object
for (var key in data) {
if (data.hasOwnProperty(key)) { // this will check if key is owned by data object and not by any of it's ancestors
alert(key+': '+data[key]); // this will show each key with it's value
}
}
回答by Shiplu Mokaddim
回答by chris
Parse it back to? You could just iterate over it as the object that it is.
解析回?您可以将其作为对象进行迭代。
Say you defined your JSON object as a variable through a closure or something, or for the sake of example just as a variable hardcoded... IE:
假设您通过闭包或其他方式将 JSON 对象定义为变量,或者仅作为示例硬编码的变量... IE:
var myJSON = "{"AboutToExpire":{"Display":true,"Message":"Several of your subscriptions are about to expire. \u003ca id=\"lnkShowExpiringSubs\" href=\"#\"\u003eView subscriptions\u003c/a\u003e"},"Expired":{"Display":true,"Message":"Your McAfee WaveSecure - Tablet Edition subscription has expired and you've been without protection for 384 days. \u003ca id=\"lnkNotificationRenewNow\" href=\"http://home.mcafee.com/root/campaign.aspx?cid=96035&pk=FAB37CF4-3680-4A87-A253-77E7D48BF6D7&affid=0\"\u003eRenew now\u003c/a\u003e"}}"
with jquery's each() you can just iterate over it like.
使用 jquery 的 each() 你可以像迭代一样迭代它。
$each(myJSON, function(x){document.print(myJSON.AboutToExpire[x].Message);`});