{"id":167030,"date":"2019-12-28T13:19:26","date_gmt":"2019-12-28T05:19:26","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=167030"},"modified":"2019-12-17T16:20:41","modified_gmt":"2019-12-17T08:20:41","slug":"python3-parse-complex","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python3-parse-complex.html","title":{"rendered":"Python3 \u89e3\u6790\u590d\u6742\u7ed3\u6784\u7684 json"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nJSON(JavaScript Object Notation)\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\u3002<\/p>\n

\u5b83\u57fa\u4e8eECMAScript\u7684\u4e00\u4e2a\u5b50\u96c6\uff0c\u6613\u4e8e\u9605\u8bfb\u548c\u7f16\u5199\u3002<\/p>\n

Python3 \u4e2d\u53ef\u4ee5\u4f7f\u7528json\u6a21\u5757\u6765\u5bf9JSON\u6570\u636e\u8fdb\u884c\u7f16\u7801\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

d1 = [{\"id\" : 1,\"name\" : \"Number1\",\"age\" : 11},{\"id\" : 2,\"name\" : \"Number2\",\"age\" : 22},{\"id\" : \"3\",\"name\" : \"Number3\",\"age\" : 33}]\r\nd2 = {\"persons\" :[{\"id\" : 1,\"name\" : \"Number1\",\"age\" : 11},{\"id\" : \"2\",\"name\" : \"Number2\",\"age\" : 22},{\"id\" : 3,\"name\" : \"Number3\",\"age\" : 33}]}\r\nd3 = {\"code\" : 200, \"persons\" :[{\"id\" : 1,\"name\" : \"Number1\",\"age\" : 11},{\"id\" : True,\"name\" : \"Number2\",\"age\" : 22},{\"id\" : 3,\"name\" : \"Number3\",\"age\" : 33}]}<\/pre>\n

## \u83b7\u53d6 json \u6570\u7ec4\u6216json \u5bf9\u8c61\u7684 key \u5217\u8868<\/p>\n

def get_json_keys(json_str,json_keys = []):\r\n    if isinstance(json_str,list):\r\n        for json_obj in json_str:\r\n            for key in json_obj.keys():\r\n                if key not in json_keys:\r\n                    json_keys.append(key)\r\n    elif isinstance(json_str,dict):\r\n        for key in json_str.keys():\r\n                if key not in json_keys:\r\n                    json_keys.append(key)\r\n    return json_keys<\/pre>\n

## \u5c06json \u6570\u7ec4\u4e2d\u76f8\u540c\u7684 key - value\u503c\u8fdb\u884c\u5408\u5e76<\/p>\n

def get_key_values(json_str,json_keys):\r\n    target_json = {}\r\n    for key in json_keys:\r\n        key_values = []\r\n        for json_obj in json_str:\r\n            if isinstance(json_obj,dict):\r\n                key_values.append(json_obj[key])\r\n        target_json[key] = key_values\r\n    return target_json<\/pre>\n

## \u4e3b\u65b9\u6cd5<\/p>\n

def analyse_json(json_str):\r\n    target_json = {}\r\n    json_keys = []\r\n    if isinstance(json_str,list):\r\n        json_keys = get_json_keys(json_str,json_keys)\r\n        target_json = get_key_values(json_str,json_keys)\r\n    elif isinstance(json_str,dict):\r\n        json_keys = get_json_keys(json_str,json_keys)\r\n        for key in json_keys:\r\n            if not isinstance(json_str[key],list) and not  isinstance(json_str[key],dict):\r\n                target_json[key] = json_str[key]\r\n            else:\r\n                target_json[key] = analyse_json(json_str[key])\r\n    return target_json\r\n\r\n\r\nprint(analyse_json(d1))\r\nprint(analyse_json(d2))\r\nprint(analyse_json(d3))<\/pre>\n

\u8f93\u51fa\uff1a<\/p>\n

{'id': [1, 2, '3'], 'name': ['Number1', 'Number2', 'Number3'], 'age': [11, 22, 33]}\r\n{'persons': {'id': [1, '2', 3], 'name': ['Number1', 'Number2', 'Number3'], 'age': [11, 22, 33]}}\r\n{'code': 200, 'persons': {'id': [1, True, 3], 'name': ['Number1', 'Number2', 'Number3'], 'age': [11, 22, 33]}}<\/pre>\n","protected":false},"excerpt":{"rendered":"

d1 = [{“id” : 1,”name” : “Number1″,”age” : 11},{“id” : […]<\/p>\n","protected":false},"author":1481,"featured_media":167032,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-167030","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167030","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1481"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=167030"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167030\/revisions"}],"predecessor-version":[{"id":167149,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167030\/revisions\/167149"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/167032"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=167030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=167030"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=167030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}