{"id":422,"date":"2019-05-27T14:50:19","date_gmt":"2019-05-27T06:50:19","guid":{"rendered":"https:\/\/www.yinyubo.cn\/?p=422"},"modified":"2022-05-17T09:42:33","modified_gmt":"2022-05-17T01:42:33","slug":"python","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=422","title":{"rendered":"python-\u68c0\u67e5\u4e00\u4e2ajson\u662f\u5426\u5305\u542b\u53e6\u5916\u4e00\u4e2ajson"},"content":{"rendered":"<h1>\u9700\u6c42\uff1a<\/h1>\n<p>\u6709\u4e24\u4e2ajson\u6587\u4ef6\u3002\u4e24\u4e2a\u90fd\u662f\u590d\u6742\u5d4c\u5957\u683c\u5f0f\u3002\u9700\u8981\u6bd4\u5bf9A.json\u91cc\u662f\u5426\u5305\u542bb.json\u3002<br \/>\n\u4f8b\u5982A.json\u662f<\/p>\n<pre class=\"lang:python decode:true EnlighterJSRAW\" data-enlighter-language=\"golang\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">{\n  \"role\": \"admin\",\n  \"routes\": [\n    \"\/Home\",\n    \"\/DeviceManagement\",\n    \"\/UserManagement\"\n  ]\n}\n<\/code><\/pre>\n<p>B.json\u662f<\/p>\n<pre class=\"lang:python decode:true EnlighterJSRAW\" data-enlighter-language=\"golang\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">{\n  \"role\": \"admin\",\n  \"routes\": [\n    \"\/Home\",\n    \"\/TemplateManagement\",\n    \"\/DataDictionary\",\n    \"\/ClassifyAndSubEntry\",\n    \"\/ProjectManagement\",\n    \"\/DeviceManagement\",\n    \"\/UserManagement\"\n  ]\n}\n<\/code><\/pre>\n<p>\u8981\u68c0\u67e5B\u6587\u4ef6\u662f\u5426\u5305\u542bA\u6587\u4ef6\u3002\u8fd9\u4e2ajson\u6587\u4ef6\u8fd8\u597d\u5d4c\u5957\u4e0d\u591a\uff0c\u5f53\u591a\u4e2adict\u548clist\u9b42\u7528\uff0c\u5373[]\u548c{}\u592a\u591a\u65f6\uff0c\u5219\u4f1a\u51fa\u73b0\u5f88\u96be\u6bd4\u5bf9\u7684\u95ee\u9898\u3002\u6211\u8003\u8651\u4f7f\u7528jsonpath\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898<\/p>\n<h1>\u4ee3\u7801\u5b9e\u73b0<\/h1>\n<p>1.\u628ajson\u6587\u4ef6\u53d8\u6210\u4e00\u4e2a\u65b0\u7684dict[jsonpath,value] \uff0c\u4f8b\u5982&#8217;infos\/0\/item&#8217;: &#8216;direction&#8217;\u00a0 \u8868\u793ajsonpath\u4e3a&#8221;infos.0.item&#8221;\u5bf9\u5e94\u7684\u503c\u662fdirection\u3002\u5177\u4f53\u7684\u53ef\u4ee5\u6253\u5370\u4e00\u4e0bJsonPathValue\u8fd9\u4e2a\u7c7b\u7684final_dict\u5c31\u80fd\u660e\u767d\u4e86<\/p>\n<pre class=\"lang:python decode:true  EnlighterJSRAW\" data-enlighter-language=\"golang\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">#  filename : test1.py\n#  description :\n#\n#  created by zhenwei.li at 2019\/5\/27 10:59\nimport json\nclass JsonPathValue(object):\n    def __init__(self, datadict):\n        self.stack = []\n        self.final_dict = {}\n        self.do_walk(datadict)\n    def get_dict(self):\n        return self.final_dict\n    def do_walk(self, datadict):\n        if isinstance(datadict, dict):\n            for key, value in datadict.items():\n                self.stack.append(key)\n                # print(\"\/\".join(self.stack))\n                if isinstance(value, dict) and len(value.keys()) == 0:\n                    self.final_dict[\"\/\".join(self.stack)] = \"EMPTY_DICT\"\n                if isinstance(value, list) and len(value) == 0:\n                    self.final_dict[\"\/\".join(self.stack)] = 'EMPTY_LIST'\n                if isinstance(value, dict):\n                    self.do_walk(value)\n                if isinstance(value, list):\n                    self.do_walk(value)\n                else:\n                    self.final_dict[\"\/\".join(self.stack)] = value\n                self.stack.pop()\n        if isinstance(datadict, list):\n            n = 0\n            for key in datadict:\n                self.stack.append(str(n))\n                n = n + 1\n                if isinstance(key, dict):\n                    self.do_walk(key)\n                if isinstance(key, list):\n                    self.do_walk(key)\n                if isinstance(key, str):\n                    self.final_dict[\"\/\".join(self.stack)] = key\n                self.stack.pop()\ndef json_contain(checkpoint, actual, assert_flag):\n    \"\"\"\n    \u68c0\u67e5\u5b9e\u9645\u7ed3\u679c(json)\u4e2d\u662f\u5426\u5305\u542b\u68c0\u67e5\u70b9(json)\u3002\u4e24\u4e2a\u5fc5\u987b\u662f\u540c\u79cd\u683c\u5f0f\uff0c\u6bd4\u5982\u540c\u65f6\u662f{}\u6216\u8005[]\n    :param checkpoint: \u68c0\u67e5\u70b9(\u671f\u671b\u7ed3\u679c)\n    :param actual:  \u5b9e\u9645\u7ed3\u679c\n    :param assert_flag: \u662f\u5426\u542f\u7528assert\u68c0\u67e5\n    :return: \u5339\u914d\u6210\u529f\u6216\u5931\u8d25\n    \"\"\"\n    result = False\n    if isinstance(checkpoint, list):\n        '''\u5982\u679c\u671f\u671b\u7684\u68c0\u67e5\u70b9\u662flist[]\u683c\u5f0f\uff0c\u4f7f\u7528\u6b64\u65b9\u6cd5\u68c0\u67e5'''\n        find_count = 0\n        check_lenth = len(checkpoint)\n        for item in checkpoint:\n            checkpoint_dict = JsonPathValue(item).get_dict()\n            if isinstance(actual, list):\n                find_flag = False\n                for actual_item in actual:\n                    actual_dict = JsonPathValue(actual_item).get_dict()\n                    find_flag = list_contain(checkpoint_dict, actual_dict, False)\n                    if find_flag:\n                        find_count += 1\n                        break\n                print(find_flag)\n            else:\n                assert False, \"\u8fd4\u56de\u7684\u5b9e\u9645\u7ed3\u679c\u683c\u5f0f\u4e0d\u6b63\u786e\"\n            if assert_flag:\n                assert find_flag, \"\u671f\u671b\u7ed3\u679c\u4e2d\u7684\\n%s\\n\u5339\u914d\u5931\u8d25\uff0c\u5b9e\u9645\u7ed3\u679c\u662f:\\n%s\" % (item, actual)\n        if find_count == check_lenth:\n            result = True\n    elif isinstance(checkpoint, dict):\n        '''\n        \u5982\u679c\u671f\u671b\u7684\u68c0\u67e5\u70b9\u662fdict{}\u683c\u5f0f\n        '''\n        checkpoint_dict = JsonPathValue(checkpoint).get_dict()\n        actual_dict = JsonPathValue(actual).get_dict()\n        if list_contain(checkpoint_dict, actual_dict, True):\n            result = True\n    return result\ndef list_contain(checkpoint_dict, actual_dict, assert_flag):\n    \"\"\"\n     \u68c0\u67e5\u5b9e\u9645\u7ed3\u679c(list)\u4e2d\u662f\u5426\u5305\u542b\u671f\u671b\u7ed3\u679c(list)\n    :param checkpoint_dict: \u5b9e\u9645\u7ed3\u679c\n    :param actual_dict: \u671f\u671b\u7ed3\u679c\n    :param assert_flag: \u662f\u5426\u542f\u7528assert\u68c0\u67e5\n    \"\"\"\n    result = set(checkpoint_dict.items()).issubset(set(actual_dict.items()))\n    if assert_flag is True:\n        different = set(checkpoint_dict.items()) - (set(actual_dict.items()))\n        assert result, \\\n            \"\u671f\u671b\u7ed3\u679c\u4e2d\u7684%s\u5339\u914d\u5931\u8d25\uff0c\u5b9e\u9645\u7ed3\u679c\u662f:\\n%s\" % (different, actual_dict)\n    return result\njson_data = open('A.json', 'rb').read()\njson_dict = json.loads(json_data)\njson_data2 = open('B.json', 'rb').read()\njson_dict2 = json.loads(json_data2)\nres1 = json_contain(json_dict, json_dict2, True)\nprint(res1)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42\uff1a \u6709\u4e24\u4e2ajson\u6587\u4ef6\u3002\u4e24\u4e2a\u90fd\u662f\u590d\u6742\u5d4c\u5957\u683c\u5f0f\u3002\u9700\u8981\u6bd4\u5bf9A.json\u91cc\u662f\u5426\u5305\u542bb.json\u3002 \u4f8b\u5982A.jso [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-422","post","type-post","status-publish","format-standard","hentry","category-k8s"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/422","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=422"}],"version-history":[{"count":2,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/422\/revisions"}],"predecessor-version":[{"id":911,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/422\/revisions\/911"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}