我正在尝试从列表制作json文本。
我如何尝试:
data = ["id": "1", "title": "2", "data": "3"]
我希望它在json编码后如何:
{"id":"1","title":"2","data":"2"}
我正在尝试为json创建列表...问题是列表以这种方式出现故障
您可以使用json.dumps
执行此操作。例如-
import json
list = ['a','b','c']
json_str = json.dumps(list)
这里如何将对象传递给json
import json
# a Python object (dict):
x = {
"name": "John",
"age": 30,
"city": "New York"
}
# convert into JSON:
y = json.dumps(x)
# the result is a JSON string:
print(y)
#{"name": "John", "age": 30, "city": "New York"}
其他在python中转换为json的人:
import json
print(json.dumps({"name": "John", "age": 30}))
print(json.dumps(["apple", "bananas"]))
print(json.dumps(("apple", "bananas")))
print(json.dumps("hello"))
print(json.dumps(42))
print(json.dumps(31.76))
print(json.dumps(True))
print(json.dumps(False))
print(json.dumps(None))
看到它:https://www.w3schools.com/python/python_json.asphttps://docs.python.org/2/library/json.html
在这种情况下不要使用列表
您需要像Python中的数据类型一样使用{key:value},您可以使用Dictionary Like:
import json
# some JSON:
x = '{ "name":"John", "age":30, "city":"New York"}'
# parse x:
y = json.loads(x)
# the result is a Python dictionary:
print(y)
将通过在python中使用json库完成
有关更多信息,请阅读文档并检查https://realpython.com/python-json/