網站首頁 學習教育 IT科技 金融知識 旅遊規劃 生活小知識 家鄉美食 養生小知識 健身運動 美容百科 遊戲知識 綜合知識
當前位置:趣知科普吧 > IT科技 > 

字元串轉json|python

欄目: IT科技 / 發佈於: / 人氣:1.86W

python中字元串怎麼轉json呢?不知道的小夥伴來看看小編今天的分享吧!

python中字元串怎麼轉json方法:

python字元串轉json對象,需要使用json模組的loads函數

# encoding: utf-8

print "str轉json"

import json

string_a = '{"accessToken": "521de21161b23988173e6f7f48f9ee96e28", "User-Agent": "Apache-HttpClient/4.5.2 (Java/1.8.0_131)"}'

print "type(string_a): ", type(string_a)

dict_a = json.loads(string_a)  # json_str == dict

print "dict_a: ", dict_a

print "type(dict_a): ", type(dict_a)

python 字元串轉json

輸出結果:

str轉json

type(string_a):  <type 'str'>

dict_a:  {u'accessToken': u'521de21161b23988173e6f7f48f9ee96e28', u'User-Agent': u'Apache-HttpClient/4.5.2 (Java/1.8.0_131)'}

type(dict_a):  <type 'dict'>

python 字元串轉json 第2張

拓展資料:

python中json轉字元串。

# encoding: utf-8

print "json轉str"

import json

json_b = {"accessToken": "521de21161b23988173e6f7f48f9ee96e28", "User-Agent": "Apache-HttpClient/4.5.2 (Java/1.8.0_131)"}

print "type(json_b): ", type(json_b)

str_b = json.dumps(json_b)

print "str_b: ", str_b

print "type(str_b): ", type(str_b)

輸出結果:

json轉str

type(json_b):  <type 'dict'>

str_b:  {"accessToken": "521de21161b23988173e6f7f48f9ee96e28", "User-Agent": "Apache-HttpClient/4.5.2 (Java/1.8.0_131)"}

type(str_b):  <type 'str'>