【Python】json与string相互转化

1 json->string

# -*- coding: utf-8 -*-
import json

student = {"id":"123", "name":"122", "age":18}
print(type(student))
print(student)

print('-'*10)

result = json.dumps(student)
print(type(result))
print(result)

2 string->json

2.2 json类

import json
str = '{"key": "wwww", "word": "qqqq"}'
j = json.loads(str)
print(j)
print(type(j))

2.1 ast类

import ast 
a = "{1: 'a', 2: 'b'}"
b = ast.literal_eval(a)
type(b)
dict

版权声明:本文为luolinll1212原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。