解析样式后分离键/值对

时间:2018-02-21 18:33:31

标签: javascript json reactjs parsing key-value

我有这个代码将一个大的JSON对象解析为它的键/值对

var origObject = {"id":3,"code":"\nfrom ipwhois import IPWhois\nimport json\nobj = IPWhois('104.24.114.36')\nresults = obj.lookup_rdap(depth=1)\nprint(json.dumps(results))\n","state":"available","output":{"status":"ok","execution_count":3,"data":{"text/plain":"{\"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\"}"}},"progress":1.0}; 
    var myTarget = origObject.output.data['text/plain'];
    var obj = JSON.parse(myTarget);
    var results = [];

    for (var key in obj) {
      if (obj.hasOwnProperty(key)) {
        results.push(key + " : " + obj[key] );
      }
    }

并在return()中显示如下

{results.map((value) => <p> {value} </p>)}

我需要能够分离键值,以便我可以设置它们的样式/可能将它们放在格式化的表格中等等...现在我只是获得了一个结果推送的转储。

3 个答案:

答案 0 :(得分:0)

您是否尝试使用Map()而不是数组?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/set

简短教程 - https://www.youtube.com/watch?v=sAJ82Ma33kM

它会让你保持键和值,并确保键是唯一的。

答案 1 :(得分:0)

map()遍历数组。你有一个对象。

你可以做的是获取对象键的数组,并在将每个键传递给对象时迭代它们。

Object.keys(obj).map(key => <p>{ obj[key] }</p>)

答案 2 :(得分:0)

工作演示

public var strMaker = ""