{"id":42,"date":"2026-02-16T07:25:56","date_gmt":"2026-02-15T23:25:56","guid":{"rendered":"https:\/\/liuyunge.net\/?p=42"},"modified":"2026-02-20T20:40:26","modified_gmt":"2026-02-20T12:40:26","slug":"jsondump-jsonload-jsonpath","status":"publish","type":"post","link":"https:\/\/liuyunge.net\/index.php\/2026\/02\/16\/jsondump-jsonload-jsonpath\/","title":{"rendered":"jsondump jsonload jsonpath"},"content":{"rendered":"\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\" open><summary><\/summary>\n<pre class=\"wp-block-code\"><code><\/code><\/pre>\n<\/details>\n\n\n\n<p id=\"jsonload-jsonloads-\u4ece\u6587\u4ef6\u91cc\uff08\u5b57\u7b26\u7c7b\u578b\uff09\uff0c\u628a\u6570\u636e\u8bfb\u53d6\u4e3ajson\u683c\u5f0f\">jsondumps jsondump \u5b57\u5178\u5b58\u6587\u4ef6\u65f6\uff0c\u5c06\u6570\u636e\u8f6c\u4e3a\u5b57\u7b26\u7c7b\u578b\uff0c\u5199\u5165\u5230\u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n# \u5bfc\u5165json\u6a21\u5757\uff0c\u7528\u4e8e\u5c06\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\nimport json\ndic = {\n    \"name\": \"\u5e7f\u4e1c\",  # \u7701\u4efd\u540d\u79f0\n    \"cities\": {  # \u57ce\u5e02\u4fe1\u606f\n        \"city\": &#91;\"\u5e7f\u5dde\", \"\u6df1\u5733\", \"\u73e0\u6d77\"]  # \u57ce\u5e02\u5217\u8868\n    }\n}\nprint(type(dic))  # \u6253\u5370dic\u7684\u7c7b\u578b\uff0c\u8f93\u51fa&lt;class 'dict'>\n# \u6253\u5f00\u6587\u4ef6\uff0c\u51c6\u5907\u5199\u5165\nwith open('\u5e7f\u4e1c.json', 'w', encoding='utf-8') as f: \n    # \u5c06dic\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u5e76\u5199\u5165\u6587\u4ef6\n    f.write(json.dumps(dic))  \nimport json\ndic = {\n    \"name\": \"\u5e7f\u4e1c\",\n    \"cities\": {\n        \"city\": &#91;\"\u5e7f\u5dde\", \"\u6df1\u5733\", \"\u73e0\u6d77\"]\n    }\n}\nprint(type(dic))  # &lt;class 'dict'>\n# \u4f7f\u7528json.dump()\u65b9\u6cd5\u5c06\u5b57\u5178dic\u4ee5JSON\u683c\u5f0f\u5199\u5165\u6587\u4ef6\n# ensure_ascii=False\u8868\u793a\u4e0d\u5c06\u4e2d\u6587\u5b57\u7b26\u8f6c\u4e49\u4e3aASCII\u7f16\u7801\n# indent=4\u8868\u793a\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u7f29\u8fdb\u4e3a4\u4e2a\u7a7a\u683c\uff0c\u4f7f\u751f\u6210\u7684JSON\u6587\u4ef6\u66f4\u6613\u8bfb\nwith open('\u5e7f\u4e1c.json', 'w', encoding='utf8') as f:\n    # f.write(json.dumps(dic, ensure_ascii=False, indent=4))  # json.dumps()\u5199\u6cd5\n    json.dump(dic, f, ensure_ascii=False, indent=4)  # json.dump()\u5199\u6cd5<\/code><\/pre>\n\n\n\n<p>jsonload jsonloads \u4ece\u6587\u4ef6\u91cc\uff08\u5b57\u7b26\u7c7b\u578b\uff09\uff0c\u628a\u6570\u636e\u8bfb\u53d6\u4e3ajson\u683c\u5f0f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\n# \u6253\u5f00\u6587\u4ef6\u201c\u5e7f\u4e1c.json\u201d\uff0c\u4ee5\u8bfb\u53d6\u6a21\u5f0f\u6253\u5f00\uff0c\u6307\u5b9a\u7f16\u7801\u4e3autf8\nwith open('\u5e7f\u4e1c.json', 'r', encoding='utf8') as f:\n    line = f.read()  # \u8bfb\u53d6\u6587\u4ef6\u7684\u5168\u90e8\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cfline\n\n# \u4f7f\u7528json.loads()\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32line\u8f6c\u6362\u4e3aJSON\u5bf9\u8c61\njson_obj = json.loads(line)\n# \u6253\u5370json_obj\u7684\u7c7b\u578b\uff0cjson_obj\u662f\u5b57\u5178\u7c7b\u578b\nprint(type(json_obj))\n# \u6253\u5370json_obj\u7684\u5185\u5bb9\uff0c\u5373\u8f6c\u6362\u540e\u7684\u5b57\u5178\u5bf9\u8c61\nprint(json_obj)\n\nimport json\n# \u6253\u5f00\u6587\u4ef6\u201c\u5e7f\u4e1c.json\u201d\uff0c\u4ee5\u8bfb\u53d6\u6a21\u5f0f\u6253\u5f00\uff0c\u6307\u5b9a\u7f16\u7801\u4e3autf8\nwith open('\u5e7f\u4e1c.json', 'r', encoding='utf8') as f:\n    json_obj = json.load(f)  # \u4f7f\u7528json.load()\u65b9\u6cd5\u76f4\u63a5\u4ece\u6587\u4ef6\u4e2d\u52a0\u8f7dJSON\u6570\u636e\n# \u6253\u5370json_obj\u7684\u7c7b\u578b\uff0cjson_obj\u662f\u5b57\u5178\u7c7b\u578b\nprint(type(json_obj))\n# \u6253\u5370json_obj\u7684\u5185\u5bb9\uff0c\u5373\u52a0\u8f7d\u540e\u7684\u5b57\u5178\u5bf9\u8c61\nprint(json_obj)<\/code><\/pre>\n\n\n\n<p id=\"jsonpath-\u89e3\u6790json\u7ed3\u6784\u7684\u6570\u636e\u89e3\u6790\u6a21\u5757\uff0c\u53ef\u4ee5\u4ecejson\u4e2d\u62bd\u53d6\u6307\u5b9a\u4fe1\u606f\u3002\">jsonpath \u89e3\u6790json\u7ed3\u6784\u7684\u6570\u636e\u89e3\u6790\u6a21\u5757\uff0c\u53ef\u4ee5\u4ecejson\u4e2d\u62bd\u53d6\u6307\u5b9a\u4fe1\u606f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5bfc\u5165jsonpath\u6a21\u5757\nimport jsonpath\n\n# \u5b9a\u4e49JSON\u6570\u636e\ndata = {\n    \"store\": {\n        \"book\": &#91;\n            {\n                \"category\": \"classics\",\n                \"author\": \"\u65bd\u8010\u5eb5\",\n                \"title\": \"\u6c34\u6d52\u4f20\",\n                \"price\": 58.00\n            },\n            {\n                \"category\": \"classics\",\n                \"author\": \"\u7f57\u8d2f\u4e2d\",\n                \"title\": \"\u4e09\u56fd\u6f14\u4e49\",\n                \"price\": 68.00\n            },\n            {\n                \"category\": \"classics\",\n                \"author\": \"\u5434\u627f\u6069\",\n                \"title\": \"\u897f\u6e38\u8bb0\",\n                \"isbn\": \"978-7-5327-6543-2\",\n                \"price\": 78.00\n            },\n            {\n                \"category\": \"classics\",\n                \"author\": \"\u66f9\u96ea\u82b9\",\n                \"title\": \"\u7ea2\u697c\u68a6\",\n                \"isbn\": \"978-7-5302-1843-4\",\n                \"price\": 88.00\n            }\n        ],\n        \"bicycle\": {\n            \"color\": \"red\",\n            \"price\": 19.95\n        }\n    }\n}\n\n# $.store.book&#91;*].author    \u6240\u6709\u4e66\u7c4d\u7684\u4f5c\u8005\nauthors = jsonpath.jsonpath(data, \"$.store.book&#91;*].author\")\nprint(\"\u6240\u6709\u4e66\u7c4d\u7684\u4f5c\u8005:\", authors)\n\n# $.store.book&#91;1].title \u7b2c\u4e8c\u672c\u4e66\u7684\u4e66\u540d\nsecond_book_title = jsonpath.jsonpath(data, \"$.store.book&#91;1].title\")\nprint(\"\u7b2c\u4e8c\u672c\u4e66\u7684\u4e66\u540d:\", second_book_title)\n\n# $..author \u6240\u6709\u4f5c\u8005\nall_authors = jsonpath.jsonpath(data, \"$..author\")\nprint(\"\u6240\u6709\u4f5c\u8005:\", all_authors)\n\n# $.store.* store\u4e0b\u7684\u6240\u6709\u5b50\u8282\u70b9\nstore_children = jsonpath.jsonpath(data, \"$.store.*\")\nprint(\"store\u4e0b\u7684\u6240\u6709\u5b50\u8282\u70b9:\", store_children)\n\n# $.store..price    \u6240\u6709\u4ef7\u683c\u5b57\u6bb5\nall_prices = jsonpath.jsonpath(data, \"$.store..price\")\nprint(\"\u6240\u6709\u4ef7\u683c\u5b57\u6bb5:\", all_prices)\n\n# $..book&#91;2]    \u7b2c\u4e09\u672c\u4e66\uff08\u7d22\u5f15\u4ece 0 \u5f00\u59cb\uff09\nthird_book = jsonpath.jsonpath(data, \"$..book&#91;2]\")\nprint(\"\u7b2c\u4e09\u672c\u4e66:\", third_book)\n\n# $..book&#91;(@.length-1)] \u6700\u540e\u4e00\u672c\u4e66\nlast_book = jsonpath.jsonpath(data, \"$..book&#91;(@.length-1)]\")\nprint(\"\u6700\u540e\u4e00\u672c\u4e66:\", last_book)\n\n# $..book&#91;?(@.isbn)]    \u542b isbn\u7684\u4e66\nbooks_with_isbn = jsonpath.jsonpath(data, \"$..book&#91;?(@.isbn)]\")\nprint(\"\u542b isbn\u7684\u4e66:\", books_with_isbn)\n\n# $..book&#91;?(@.price&lt;10)]    \u4ef7\u683c\u5c0f\u4e8e 10 \u7684\u4e66\nbooks_with_price_less_than_10 = jsonpath.jsonpath(data, \"$..book&#91;?(@.price&lt;10)]\")\nprint(\"\u4ef7\u683c\u5c0f\u4e8e 10 \u7684\u4e66:\", books_with_price_less_than_10)\n\n# $..*  \u9012\u5f52\u5339\u914d\u6240\u6709\u5b50\u8282\u70b9\nall_nodes = jsonpath.jsonpath(data, \"$..*\")\nprint(\"\u9012\u5f52\u5339\u914d\u6240\u6709\u5b50\u8282\u70b9:\", all_nodes)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>jsondumps jsondump \u5b57\u5178\u5b58\u6587\u4ef6\u65f6\uff0c\u5c06\u6570\u636e\u8f6c\u4e3a\u5b57\u7b26\u7c7b\u578b\uff0c\u5199\u5165\u5230\u6587\u4ef6 jsonload jsonloads \u4ece\u6587\u4ef6\u91cc\uff08\u5b57\u7b26\u7c7b\u578b\uff09\uff0c\u628a\u6570\u636e\u8bfb\u53d6\u4e3ajs&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/liuyunge.net\/index.php\/2026\/02\/16\/jsondump-jsonload-jsonpath\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-42","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/liuyunge.net\/index.php\/wp-json\/wp\/v2\/posts\/42","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/liuyunge.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/liuyunge.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/liuyunge.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/liuyunge.net\/index.php\/wp-json\/wp\/v2\/comments?post=42"}],"version-history":[{"count":3,"href":"https:\/\/liuyunge.net\/index.php\/wp-json\/wp\/v2\/posts\/42\/revisions"}],"predecessor-version":[{"id":52,"href":"https:\/\/liuyunge.net\/index.php\/wp-json\/wp\/v2\/posts\/42\/revisions\/52"}],"wp:attachment":[{"href":"https:\/\/liuyunge.net\/index.php\/wp-json\/wp\/v2\/media?parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/liuyunge.net\/index.php\/wp-json\/wp\/v2\/categories?post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/liuyunge.net\/index.php\/wp-json\/wp\/v2\/tags?post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}