{"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
\u641e\u61c2Python\u5b58\u50a8\u548c\u4ea4\u6362\u6570\u636e\u7684\u7528\u6cd5<\/span>\n <\/div>\n json.dumps(): \u5bf9\u6570\u636e\u8fdb\u884c\u7f16\u7801\u3002<\/span> dict<\/span> <\/body> \u5bfc\u5165 json \u6a21\u5757<\/span>
import json<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\nJSON \u6570\u636e\u89e3\u6790<\/h2>\n
\n json.loads(): \u5bf9\u6570\u636e\u8fdb\u884c\u89e3\u7801\u3002<\/span> <\/p>\n\n\n
\n Python<\/td>\n JSON<\/td>\n<\/tr>\n \n dict<\/td>\n object<\/td>\n<\/tr>\n \n list, tuple<\/td>\n array<\/td>\n<\/tr>\n \n str<\/td>\n string<\/td>\n<\/tr>\n \n int, float, int- & float-derived Enums<\/td>\n number<\/td>\n<\/tr>\n \n True<\/td>\n true<\/td>\n<\/tr>\n \n False<\/td>\n false<\/td>\n<\/tr>\n \n None<\/td>\n null<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n \n\n
\n JSON<\/td>\n Python<\/td>\n<\/tr>\n \n object<\/td>\n dict<\/td>\n<\/tr>\n \n array<\/td>\n list<\/td>\n<\/tr>\n \n string<\/td>\n str<\/td>\n<\/tr>\n \n number (int)<\/td>\n int<\/td>\n<\/tr>\n \n number (real)<\/td>\n float<\/td>\n<\/tr>\n \n true<\/td>\n True<\/td>\n<\/tr>\n \n false<\/td>\n False<\/td>\n<\/tr>\n \n null<\/td>\n None<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n \u89e3\u6790 JSON - \u628a JSON \u8f6c\u6362\u4e3a Python<\/h2>\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['name']<\/span><\/code><\/pre>\n<\/p><\/div>\n
\u628a Python \u8f6c\u6362\u4e3a JSON<\/h2>\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{\"name\": \"Jack\", \"age\": 26, \"sex\": \"woman\"}<\/span><\/code><\/pre>\n<\/p><\/div>\n
\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 # \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{\"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
# \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{
\"age\": 26,
\"name\": \"Jack\",
\"sex\": \"woman\"
}<\/span><\/code><\/pre>\n<\/p><\/div>\n\u5b9a\u4e49\u5206\u9694\u7b26<\/h2>\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{
\"age\" = 26,
\"name\" = \"Jack\",
\"sex\" = \"woman\"
}<\/span><\/code><\/pre>\n<\/p><\/div>\n\u5bf9\u7ed3\u679c\u6392\u5e8f<\/h2>\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{
\"age\" = 26,
\"name\" = \"Jack\",
\"sex\" = \"woman\"
}<\/span><\/code><\/pre>\n<\/p><\/div>\n
\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}]}}