我有这个大型的javascript对象:
{"raw": null, "entities": ["CLOUD14", "ABUSE2916-ARIN", "ADMIN2521-ARIN", "NOC11962-ARIN"], "asn_registry": "arin", "network": {"status": null, "handle": "NET-104-16-0-0-1", "name": "CLOUDFLARENET", "links": ["https://rdap.arin.net/registry/ip/104.16.0.0", "https://whois.arin.net/rest/net/NET-104-16-0-0-1"], "raw": null, "country": null, "ip_version": "v4", "start_address": "104.16.0.0", "notices": [{"description": "By using the ARIN RDAP/Whois service, you are agreeing to the RDAP/Whois Terms of Use", "links": ["https://www.arin.net/whois_tou.html"], "title": "Terms of Service"}], "end_address": "104.31.255.255", "remarks": [], "parent_handle": "NET-104-0-0-0-0", "cidr": "104.16.0.0/12", "type": null, "events": [{"action": "last changed", "timestamp": "2017-02-17T18:08:30-05:00", "actor": null}, {"action": "registration", "timestamp": "2014-03-28T10:30:55-04:00", "actor": null}]}, "objects": {"CLOUD14": {"status": null, "roles": ["registrant"], "handle": "CLOUD14", "entities": ["ABUSE2916-ARIN", "ADMIN2521-ARIN", "NOC11962-ARIN"], "links": ["https://rdap.arin.net/registry/entity/CLOUD14", "https://whois.arin.net/rest/org/CLOUD14"], "raw": null, "notices": null, "contact": {"kind": "org", "name": "Cloudflare, Inc.", "title": null, "phone": null, "role": null, "address": [{"type": null, "value": "101 Townsend Street\nSan Francisco\nCA\n94107\nUnited States"}], "email": null}, "events_actor": null, "remarks": [], "events": [{"action": "last changed", "timestamp": "2017-02-17T18:19:16-05:00", "actor": null}, {"action": "registration", "timestamp": "2010-07-09T13:10:42-04:00", "actor": null}]}, "ADMIN2521-ARIN": {"status": ["validated"], "roles": ["technical"], "handle": "ADMIN2521-ARIN", "entities": null, "links": ["https://rdap.arin.net/registry/entity/ADMIN2521-ARIN", "https://whois.arin.net/rest/poc/ADMIN2521-ARIN"], "raw": null, "notices": null, "contact": {"kind": "group", "name": "Admin", "title": null, "phone": [{"type": ["work", "voice"], "value": "+1-650-319-8930"}], "role": null, "address": [{"type": null, "value": "101 Townsend Street\nSan Francisco\nCA\n94107\nUnited States"}], "email": [{"type": null, "value": "rir@cloudflare.com"}]}, "events_actor": null, "remarks": [], "events": [{"action": "last changed", "timestamp": "2017-10-05T15:42:30-04:00", "actor": null}, {"action": "registration", "timestamp": "2011-04-19T15:11:36-04:00", "actor": null}]}, "ABUSE2916-ARIN": {"status": ["validated"], "roles": ["abuse"], "handle": "ABUSE2916-ARIN", "entities": null, "links": ["https://rdap.arin.net/registry/entity/ABUSE2916-ARIN", "https://whois.arin.net/rest/poc/ABUSE2916-ARIN"], "raw": null, "notices": null, "contact": {"kind": "group", "name": "Abuse", "title": null, "phone": [{"type": ["work", "voice"], "value": "+1-650-319-8930"}], "role": null, "address": [{"type": null, "value": "101 Townsend Street\nSan Francisco\nCA\n94107\nUnited States"}], "email": [{"type": null, "value": "abuse@cloudflare.com"}]}, "events_actor": null, "remarks": [], "events": [{"action": "last changed", "timestamp": "2017-02-17T18:11:38-05:00", "actor": null}, {"action": "registration", "timestamp": "2011-02-14T19:00:47-05:00", "actor": null}]}, "NOC11962-ARIN": {"status": ["validated"], "roles": ["noc"], "handle": "NOC11962-ARIN", "entities": null, "links": ["https://rdap.arin.net/registry/entity/NOC11962-ARIN", "https://whois.arin.net/rest/poc/NOC11962-ARIN"], "raw": null, "notices": null, "contact": {"kind": "group", "name": "NOC", "title": null, "phone": [{"type": ["work", "voice"], "value": "+1-650-319-8930"}], "role": null, "address": [{"type": null, "value": "101 Townsend Street\nSan Francisco\nCA\n94107\nUnited States"}], "email": [{"type": null, "value": "noc@cloudflare.com"}]}, "events_actor": null, "remarks": [], "events": [{"action": "last changed", "timestamp": "2017-02-17T18:15:44-05:00", "actor": null}, {"action": "registration", "timestamp": "2011-04-19T15:25:31-04:00", "actor": null}]}}, "asn_country_code": "US", "asn_date": "2014-03-28", "asn_cidr": "104.24.112.0/20", "nir": null, "query": "104.24.114.36", "asn": "13335"}
我正在尝试将键值对解析/转储到仪表板上的表中的列表中,但是当我执行for循环时,例如
for (var key in myObject) {
if (myObject.hasOwnProperty(key)) {
console.log(key + " -> " + myObject[key]);
}
}
不幸的是我的输出变成了
0 -> { 1 -> " 2 -> r 3 -> a 4 -> w 5 -> " 6 -> : 7 -> 8 -> n ...
我似乎将每个第一个字母与一个键配对。我到底错在了什么?我已经尝试了几个for循环(写不同的方式),但我似乎得到了相同的输出。如果您将对象转储到此站点:http://countwordsfree.com/jsonviewer并选择"树模式"故障类似于我想看到的(即使我不关心手风琴/树木)。
任何帮助将不胜感激!感谢
答案 0 :(得分:0)
您似乎正在尝试迭代
JSON string
而不是JSON Object
。
使用JSON字符串输出:
var myObject = '{"raw": null, "entities": ["CLOUD14", "ABUSE2916-ARIN", "ADMIN2521-ARIN", "NOC11962-ARIN"], "asn_registry": "arin", "network": {"status": null, "handle": "NET-104-16-0-0-1", "name": "CLOUDFLARENET", "links": ["https://rdap.arin.net/registry/ip/104.16.0.0", "https://whois.arin.net/rest/net/NET-104-16-0-0-1"], "raw": null, "country": null, "ip_version": "v4", "start_address": "104.16.0.0", "notices": [{"description": "By using the ARIN RDAP/Whois service, you are agreeing to the RDAP/Whois Terms of Use", "links": ["https://www.arin.net/whois_tou.html"], "title": "Terms of Service"}], "end_address": "104.31.255.255", "remarks": [], "parent_handle": "NET-104-0-0-0-0", "cidr": "104.16.0.0/12", "type": null, "events": [{"action": "last changed", "timestamp": "2017-02-17T18:08:30-05:00", "actor": null}, {"action": "registration", "timestamp": "2014-03-28T10:30:55-04:00", "actor": null}]}, "objects": {"CLOUD14": {"status": null, "roles": ["registrant"], "handle": "CLOUD14", "entities": ["ABUSE2916-ARIN", "ADMIN2521-ARIN", "NOC11962-ARIN"], "links": ["https://rdap.arin.net/registry/entity/CLOUD14", "https://whois.arin.net/rest/org/CLOUD14"], "raw": null, "notices": null, "contact": {"kind": "org", "name": "Cloudflare, Inc.", "title": null, "phone": null, "role": null, "address": [{"type": null, "value": "101 Townsend Street\nSan Francisco\nCA\n94107\nUnited States"}], "email": null}, "events_actor": null, "remarks": [], "events": [{"action": "last changed", "timestamp": "2017-02-17T18:19:16-05:00", "actor": null}, {"action": "registration", "timestamp": "2010-07-09T13:10:42-04:00", "actor": null}]}, "ADMIN2521-ARIN": {"status": ["validated"], "roles": ["technical"], "handle": "ADMIN2521-ARIN", "entities": null, "links": ["https://rdap.arin.net/registry/entity/ADMIN2521-ARIN", "https://whois.arin.net/rest/poc/ADMIN2521-ARIN"], "raw": null, "notices": null, "contact": {"kind": "group", "name": "Admin", "title": null, "phone": [{"type": ["work", "voice"], "value": "+1-650-319-8930"}], "role": null, "address": [{"type": null, "value": "101 Townsend Street\nSan Francisco\nCA\n94107\nUnited States"}], "email": [{"type": null, "value": "rir@cloudflare.com"}]}, "events_actor": null, "remarks": [], "events": [{"action": "last changed", "timestamp": "2017-10-05T15:42:30-04:00", "actor": null}, {"action": "registration", "timestamp": "2011-04-19T15:11:36-04:00", "actor": null}]}, "ABUSE2916-ARIN": {"status": ["validated"], "roles": ["abuse"], "handle": "ABUSE2916-ARIN", "entities": null, "links": ["https://rdap.arin.net/registry/entity/ABUSE2916-ARIN", "https://whois.arin.net/rest/poc/ABUSE2916-ARIN"], "raw": null, "notices": null, "contact": {"kind": "group", "name": "Abuse", "title": null, "phone": [{"type": ["work", "voice"], "value": "+1-650-319-8930"}], "role": null, "address": [{"type": null, "value": "101 Townsend Street\nSan Francisco\nCA\n94107\nUnited States"}], "email": [{"type": null, "value": "abuse@cloudflare.com"}]}, "events_actor": null, "remarks": [], "events": [{"action": "last changed", "timestamp": "2017-02-17T18:11:38-05:00", "actor": null}, {"action": "registration", "timestamp": "2011-02-14T19:00:47-05:00", "actor": null}]}, "NOC11962-ARIN": {"status": ["validated"], "roles": ["noc"], "handle": "NOC11962-ARIN", "entities": null, "links": ["https://rdap.arin.net/registry/entity/NOC11962-ARIN", "https://whois.arin.net/rest/poc/NOC11962-ARIN"], "raw": null, "notices": null, "contact": {"kind": "group", "name": "NOC", "title": null, "phone": [{"type": ["work", "voice"], "value": "+1-650-319-8930"}], "role": null, "address": [{"type": null, "value": "101 Townsend Street\nSan Francisco\nCA\n94107\nUnited States"}], "email": [{"type": null, "value": "noc@cloudflare.com"}]}, "events_actor": null, "remarks": [], "events": [{"action": "last changed", "timestamp": "2017-02-17T18:15:44-05:00", "actor": null}, {"action": "registration", "timestamp": "2011-04-19T15:25:31-04:00", "actor": null}]}}, "asn_country_code": "US", "asn_date": "2014-03-28", "asn_cidr": "104.24.112.0/20", "nir": null, "query": "104.24.114.36", "asn": "13335"}';
for (var key in myObject) {
if (myObject.hasOwnProperty(key)) {
console.log(key + " -> " + myObject[key]);
}
}
使用JSON对象输出:
var myObject = {"raw": null, "entities": ["CLOUD14", "ABUSE2916-ARIN", "ADMIN2521-ARIN", "NOC11962-ARIN"], "asn_registry": "arin", "network": {"status": null, "handle": "NET-104-16-0-0-1", "name": "CLOUDFLARENET", "links": ["https://rdap.arin.net/registry/ip/104.16.0.0", "https://whois.arin.net/rest/net/NET-104-16-0-0-1"], "raw": null, "country": null, "ip_version": "v4", "start_address": "104.16.0.0", "notices": [{"description": "By using the ARIN RDAP/Whois service, you are agreeing to the RDAP/Whois Terms of Use", "links": ["https://www.arin.net/whois_tou.html"], "title": "Terms of Service"}], "end_address": "104.31.255.255", "remarks": [], "parent_handle": "NET-104-0-0-0-0", "cidr": "104.16.0.0/12", "type": null, "events": [{"action": "last changed", "timestamp": "2017-02-17T18:08:30-05:00", "actor": null}, {"action": "registration", "timestamp": "2014-03-28T10:30:55-04:00", "actor": null}]}, "objects": {"CLOUD14": {"status": null, "roles": ["registrant"], "handle": "CLOUD14", "entities": ["ABUSE2916-ARIN", "ADMIN2521-ARIN", "NOC11962-ARIN"], "links": ["https://rdap.arin.net/registry/entity/CLOUD14", "https://whois.arin.net/rest/org/CLOUD14"], "raw": null, "notices": null, "contact": {"kind": "org", "name": "Cloudflare, Inc.", "title": null, "phone": null, "role": null, "address": [{"type": null, "value": "101 Townsend Street\nSan Francisco\nCA\n94107\nUnited States"}], "email": null}, "events_actor": null, "remarks": [], "events": [{"action": "last changed", "timestamp": "2017-02-17T18:19:16-05:00", "actor": null}, {"action": "registration", "timestamp": "2010-07-09T13:10:42-04:00", "actor": null}]}, "ADMIN2521-ARIN": {"status": ["validated"], "roles": ["technical"], "handle": "ADMIN2521-ARIN", "entities": null, "links": ["https://rdap.arin.net/registry/entity/ADMIN2521-ARIN", "https://whois.arin.net/rest/poc/ADMIN2521-ARIN"], "raw": null, "notices": null, "contact": {"kind": "group", "name": "Admin", "title": null, "phone": [{"type": ["work", "voice"], "value": "+1-650-319-8930"}], "role": null, "address": [{"type": null, "value": "101 Townsend Street\nSan Francisco\nCA\n94107\nUnited States"}], "email": [{"type": null, "value": "rir@cloudflare.com"}]}, "events_actor": null, "remarks": [], "events": [{"action": "last changed", "timestamp": "2017-10-05T15:42:30-04:00", "actor": null}, {"action": "registration", "timestamp": "2011-04-19T15:11:36-04:00", "actor": null}]}, "ABUSE2916-ARIN": {"status": ["validated"], "roles": ["abuse"], "handle": "ABUSE2916-ARIN", "entities": null, "links": ["https://rdap.arin.net/registry/entity/ABUSE2916-ARIN", "https://whois.arin.net/rest/poc/ABUSE2916-ARIN"], "raw": null, "notices": null, "contact": {"kind": "group", "name": "Abuse", "title": null, "phone": [{"type": ["work", "voice"], "value": "+1-650-319-8930"}], "role": null, "address": [{"type": null, "value": "101 Townsend Street\nSan Francisco\nCA\n94107\nUnited States"}], "email": [{"type": null, "value": "abuse@cloudflare.com"}]}, "events_actor": null, "remarks": [], "events": [{"action": "last changed", "timestamp": "2017-02-17T18:11:38-05:00", "actor": null}, {"action": "registration", "timestamp": "2011-02-14T19:00:47-05:00", "actor": null}]}, "NOC11962-ARIN": {"status": ["validated"], "roles": ["noc"], "handle": "NOC11962-ARIN", "entities": null, "links": ["https://rdap.arin.net/registry/entity/NOC11962-ARIN", "https://whois.arin.net/rest/poc/NOC11962-ARIN"], "raw": null, "notices": null, "contact": {"kind": "group", "name": "NOC", "title": null, "phone": [{"type": ["work", "voice"], "value": "+1-650-319-8930"}], "role": null, "address": [{"type": null, "value": "101 Townsend Street\nSan Francisco\nCA\n94107\nUnited States"}], "email": [{"type": null, "value": "noc@cloudflare.com"}]}, "events_actor": null, "remarks": [], "events": [{"action": "last changed", "timestamp": "2017-02-17T18:15:44-05:00", "actor": null}, {"action": "registration", "timestamp": "2011-04-19T15:25:31-04:00", "actor": null}]}}, "asn_country_code": "US", "asn_date": "2014-03-28", "asn_cidr": "104.24.112.0/20", "nir": null, "query": "104.24.114.36", "asn": "13335"};
for (var key in myObject) {
if (myObject.hasOwnProperty(key)) {
console.log(key + " -> " + myObject[key]);
}
}