{"id":1040,"date":"2023-03-24T09:47:07","date_gmt":"2023-03-24T01:47:07","guid":{"rendered":""},"modified":"2023-03-24T09:47:07","modified_gmt":"2023-03-24T01:47:07","slug":"Python JSON","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1040.html","title":{"rendered":"Python JSON"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Python JSON<\/h1>\n

\u641e\u61c2Python\u5b58\u50a8\u548c\u4ea4\u6362\u6570\u636e\u7684\u7528\u6cd5<\/span>\n <\/div>\n

\n JSON \u662f\u7528\u4e8e\u5b58\u50a8\u548c\u4ea4\u6362\u6570\u636e\u7684\u8bed\u6cd5\u3002JSON \u662f\u7528 JavaScript \u5bf9\u8c61\u8868\u793a\u6cd5\uff08JavaScript object notation\uff09\u7f16\u5199\u7684\u6587\u672c\u3002\n <\/div>\n
\n Python \u4e2d\u7684 JSON\uff0cPython \u6709\u4e00\u4e2a\u540d\u4e3a json \u7684\u5185\u7f6e\u5305\uff0c\u53ef\u7528\u4e8e\u5904\u7406 JSON \u6570\u636e\u3002\u5177\u4f53\u7528\u6cd5\u5982\u4e0b\uff1a\n <\/div>\n
\n
 \u5bfc\u5165 json \u6a21\u5757<\/span>
import json<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n

JSON \u6570\u636e\u89e3\u6790<\/h2>\n
\n Python3 \u4e2d\u53ef\u4ee5\u4f7f\u7528 json \u6a21\u5757\u6765\u5bf9 JSON \u6570\u636e\u8fdb\u884c\u7f16\u89e3\u7801\uff0c\u5b83\u5305\u542b\u4e86\u4e24\u4e2a\u51fd\u6570\uff1a\n <\/div>\n

json.dumps(): \u5bf9\u6570\u636e\u8fdb\u884c\u7f16\u7801\u3002<\/span>
\n json.loads(): \u5bf9\u6570\u636e\u8fdb\u884c\u89e3\u7801\u3002<\/span> <\/p>\n

\n \u5728json\u7684\u7f16\u89e3\u7801\u8fc7\u7a0b\u4e2d\uff0cpython \u7684\u539f\u59cb\u7c7b\u578b\u4e0ejson\u7c7b\u578b\u4f1a\u76f8\u4e92\u8f6c\u6362\uff0c\u5177\u4f53\u7684\u8f6c\u5316\u5bf9\u7167\u5982\u4e0b\uff1a\n <\/div>\n
\n Python \u7f16\u7801\u4e3a JSON \u7c7b\u578b\u8f6c\u6362\u5bf9\u5e94\u8868\uff1a\n <\/div>\n\n\n\n\n\n\n\n\n\n\n
Python<\/td>\nJSON<\/td>\n<\/tr>\n
dict<\/td>\nobject<\/td>\n<\/tr>\n
list, tuple<\/td>\narray<\/td>\n<\/tr>\n
str<\/td>\nstring<\/td>\n<\/tr>\n
int, float, int- & float-derived Enums<\/td>\nnumber<\/td>\n<\/tr>\n
True<\/td>\ntrue<\/td>\n<\/tr>\n
False<\/td>\nfalse<\/td>\n<\/tr>\n
None<\/td>\nnull<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\n JSON \u89e3\u7801\u4e3a Python \u7c7b\u578b\u8f6c\u6362\u5bf9\u5e94\u8868\uff1a\n <\/div>\n\n\n\n\n\n\n\n\n\n\n\n
JSON<\/td>\nPython<\/td>\n<\/tr>\n
object<\/td>\ndict<\/td>\n<\/tr>\n
array<\/td>\nlist<\/td>\n<\/tr>\n
string<\/td>\nstr<\/td>\n<\/tr>\n
number (int)<\/td>\nint<\/td>\n<\/tr>\n
number (real)<\/td>\nfloat<\/td>\n<\/tr>\n
true<\/td>\nTrue<\/td>\n<\/tr>\n
false<\/td>\nFalse<\/td>\n<\/tr>\n
null<\/td>\nNone<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u89e3\u6790 JSON - \u628a JSON \u8f6c\u6362\u4e3a Python<\/h2>\n
\n \u82e5\u6709 JSON \u5b57\u7b26\u4e32\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 json.loads() \u65b9\u6cd5\u5bf9\u5176\u8fdb\u884c\u89e3\u6790\u3002\n <\/div>\n
\n \u7ed3\u679c\u5c06\u662f Python \u5b57\u5178\u3002\n <\/div>\n
\n \u5b9e\u4f8b:\u628a JSON \u8f6c\u6362\u4e3a Python\uff1a\n <\/div>\n
\n
 # \u5bfc\u5165 json \u6a21\u5757<\/span>
import json<\/span>
x = '{ \"name\":\"Jack\", \"age\":26, \"sex\":\"woman\"}'<\/span>

# \u89e3\u6790 x:<\/span>
y = json.loads(x)<\/span>

# \u7ed3\u679c\u662f Python \u5b57\u5178\uff1a<\/span>
print([\"name\"]<\/span>)

<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
['name']<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u628a Python \u8f6c\u6362\u4e3a JSON<\/h2>\n
\n \u628a Python \u8f6c\u6362\u4e3a JSON\n <\/div>\n
\n \u82e5\u6709 Python \u5bf9\u8c61\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 json.dumps() \u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\u3002\n <\/div>\n
\n \u5b9e\u4f8b\uff1a\u628a Python \u8f6c\u6362\u4e3a JSON\uff1a\n <\/div>\n
\n
 # \u5bfc\u5165 json \u6a21\u5757<\/span>
import json<\/span>
x = { \"name\":\"Jack\", \"age\":26, \"sex\":\"woman\"}<\/span>

# \u8f6c\u6362\u4e3a JSON\uff1a<\/span>
y = json.dumps(x)<\/span>

# \u7ed3\u679c\u662f JSON \u5b57\u7b26\u4e32\uff1a<\/span>
print(y<\/span>)

<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
{\"name\": \"Jack\", \"age\": 26, \"sex\": \"woman\"}<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8fd8\u53ef\u4ee5\u628a\u4ee5\u4e0b\u7c7b\u578b\u7684 Python \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\uff1a\n <\/div>\n

dict<\/span>
\n list<\/span>
\n tuple<\/span>
\n string<\/span>
\n int<\/span>
\n float<\/span>
\n True<\/span>
\n False<\/span>
\n None<\/span> <\/p>\n

\n \u5b9e\u4f8b\uff1a\u5c06 Python \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\uff0c\u5e76\u6253\u5370\u503c\uff1a\n <\/div>\n
\n
 # \u5bfc\u5165 json \u6a21\u5757<\/span>
import json<\/span>
print(json.dumps({\"name\": \"Jack\", \"age\": 18})<\/span>)
print(json.dumps([\"apple\", \"bananas\"])<\/span>)
print(json.dumps((\"apple\", \"bananas\"))<\/span>)
print(json.dumps(\"hello\")<\/span>)
print(json.dumps(42)<\/span>)
print(json.dumps(31.76)<\/span>)
print(json.dumps(True)<\/span>)
print(json.dumps(False)<\/span>)
print(json.dumps(None)<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
{\"age\": 18, \"name\": \"Jack\"}
[\"apple\", \"bananas\"]
[\"apple\", \"bananas\"]
\"hello\"
42
31.76
true
false
null<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u683c\u5f0f\u5316\u7ed3\u679c<\/h2>\n
\n \u4e0a\u9762\u7684\u5b9e\u4f8b\u6253\u5370\u4e00\u4e2a JSON \u5b57\u7b26\u4e32\uff0c\u4f46\u5b83\u4e0d\u662f\u5f88\u5bb9\u6613\u9605\u8bfb\uff0c\u6ca1\u6709\u7f29\u8fdb\u548c\u6362\u884c\u3002\n <\/div>\n
\n json.dumps() \u65b9\u6cd5\u63d0\u4f9b\u4e86\u4ee4\u7ed3\u679c\u66f4\u6613\u8bfb\u7684\u53c2\u6570\uff1a\n <\/div>\n
\n \u5b9e\u4f8b:\u4f7f\u7528 indent \u53c2\u6570\u5b9a\u4e49\u7f29\u8fdb\u6570\uff1a\n <\/div>\n
\n
 # \u5bfc\u5165 json \u6a21\u5757<\/span>
import json<\/span>
x = { \"name\":\"Jack\", \"age\":26, \"sex\":\"woman\"}<\/span>

# \u683c\u5f0f\u5316JSON\u5b57\u7b26\u4e32<\/span>
print(json.dumps(x, indent=4)<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
{
    \"age\": 26,
    \"name\": \"Jack\",
    \"sex\": \"woman\"
}<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u5b9a\u4e49\u5206\u9694\u7b26<\/h2>\n
\n \u60a8\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u5206\u9694\u7b26\uff0c\u9ed8\u8ba4\u503c\u4e3a\uff08\", \", \": \"\uff09\uff0c\u8fd9\u610f\u5473\u7740\u4f7f\u7528\u9017\u53f7\u548c\u7a7a\u683c\u5206\u9694\u6bcf\u4e2a\u5bf9\u8c61\uff0c\u4f7f\u7528\u5192\u53f7\u548c\u7a7a\u683c\u5c06\u952e\u4e0e\u503c\u5206\u5f00\uff1a\n <\/div>\n
\n \u5b9e\u4f8b:\u4f7f\u7528 separators \u53c2\u6570\u6765\u66f4\u6539\u9ed8\u8ba4\u5206\u9694\u7b26\uff1a\n <\/div>\n
\n
 # \u5bfc\u5165 json \u6a21\u5757<\/span>
import json<\/span>
x = { \"name\":\"Jack\", \"age\":26, \"sex\":\"woman\"}<\/span>

# \u5206\u9694JSON\u5b57\u7b26\u4e32<\/span>
print(json.dumps(x, indent=4, separators=(\". \", \" = \"))<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
{
    \"age\" = 26,
    \"name\" = \"Jack\",
    \"sex\" = \"woman\"
}<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u5bf9\u7ed3\u679c\u6392\u5e8f<\/h2>\n
\n json.dumps() \u65b9\u6cd5\u63d0\u4f9b\u4e86\u5bf9\u7ed3\u679c\u4e2d\u7684\u952e\u8fdb\u884c\u6392\u5e8f\u7684\u53c2\u6570\uff1a\n <\/div>\n
\n \u5b9e\u4f8b\uff1a\u4f7f\u7528 sort_keys \u53c2\u6570\u6765\u6307\u5b9a\u662f\u5426\u5e94\u5bf9\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\uff1a\n <\/div>\n
\n
 # \u5bfc\u5165 json \u6a21\u5757<\/span>
import json<\/span>
x = { \"name\":\"Jack\", \"age\":26, \"sex\":\"woman\"}<\/span>

# \u5206\u9694JSON\u5b57\u7b26\u4e32<\/span>
print(json.dumps(x, indent=4, sort_keys=True)<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
{
    \"age\" = 26,
    \"name\" = \"Jack\",
    \"sex\" = \"woman\"
}<\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python JSONzh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[],"class_list":["post-1040","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1040"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1040"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1040\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}