我是Python的新手,正在处理JSON数据。我想通过向现有JSON对象添加一些键值对来动态构建一个JSON对象。
我尝试了以下代码,但是出现了TypeError:'str' object does not support item assignment
错误:
import json
json_data = json.dumps({})
json_data["key"] = "value"
print 'JSON: ', json_data
在将对象编码为 JSON 字符串之前,您需要先构建该对象:
import json
data = {}
data['key'] = 'value'
json_data = json.dumps(data)
JSON是一种序列化格式,它使用文本数据来表示结构。它本身并不是那个结构。
my_json_string = json.dumps({'key1': val1, 'key2': val2})
已经提供了一种解决方案,允许构建字典(或更复杂数据的嵌套字典),但如果您希望构建对象,则可以尝试使用'ObjDict'。这可大大控制将要创建的JSON,例如保留顺序,并允许构建为首选表示概念的对象。
首先请安装ObjDict:pip install objdict。
from objdict import ObjDict
data = ObjDict()
data.key = 'value'
json_data = data.dumps()
json.dumps
时可以按字母顺序排序,即json.dumps(response, sort_keys=True, indent=4)
,但你将被困在字母顺序而不是你所期望的顺序、分组和表示上。 - Jonathanjson.loads
接收一个字符串作为输入并返回一个字典作为输出。json.dumps
接收一个字典作为输入并返回一个字符串作为输出。如果需要将JSON数据转换为Python对象,可以使用Python3
中的SimpleNamespace
和object_hook
在一行代码中完成,无需额外安装:
import json
from types import SimpleNamespace
string = '{"foo":3, "bar":{"x":1, "y":2}}'
# Parse JSON into an object with attributes corresponding to dict keys.
x = json.loads(string, object_hook=lambda d: SimpleNamespace(**d))
print(x.foo)
print(x.bar.x)
print(x.bar.y)
输出:
3
1
2
JSON对象:data.json
{
"foo": 3,
"bar": {
"x": 1,
"y": 2
}
}
import json
from types import SimpleNamespace
with open("data.json") as fh:
string = fh.read()
# Parse JSON into an object with attributes corresponding to dict keys.
x = json.loads(string, object_hook=lambda d: SimpleNamespace(**d))
print(x.foo)
print(x.bar.x)
print(x.bar.y)
输出:
3
1
2
import json
from types import SimpleNamespace
import requests
r = requests.get('https://api.github.com/users/MilovanTomasevic')
# Parse JSON into an object with attributes corresponding to dict keys.
x = json.loads(r.text, object_hook=lambda d: SimpleNamespace(**d))
print(x.name)
print(x.company)
print(x.blog)
输出:
Milovan Tomašević
NLB
milovantomasevic.com
为了更漂亮、更快地访问API的JSON响应,请查看此响应。
EasyDict
库(文档):EasyDict允许像属性一样访问字典值(递归地工作)。它提供了类似JavaScript的属性点表示法,用于Python字典。
用法
>>> from easydict import EasyDict as edict
>>> d = edict({'foo':3, 'bar':{'x':1, 'y':2}})
>>> d.foo
3
>>> d.bar.x
1
>>> d = edict(foo=3)
>>> d.foo
3
[安装]: pip install easydict
所有之前的答案都是正确的,这里提供一种更简单的方法。例如,创建一个Dict数据结构来序列化和反序列化对象。
(注意:在Python中,None表示Null,我有意使用它来演示如何存储null并将其转换为JSON null)
import json
print('serialization')
myDictObj = { "name":"John", "age":30, "car":None }
##convert object to json
serialized= json.dumps(myDictObj, sort_keys=True, indent=3)
print(serialized)
## now we are gonna convert json to object
deserialization=json.loads(serialized)
print(deserialization)
myjson={}
myjson["Country"]= {"KR": { "id": "220", "name": "South Korea"}}
myjson["Creative"]= {
"1067405": {
"id": "1067405",
"url": "https://cdn.gowadogo.com/559d1ba1-8d50-4c7f-b3f5-d80f918006e0.jpg"
},
"1067406": {
"id": "1067406",
"url": "https://cdn.gowadogo.com/3799a70d-339c-4ecb-bc1f-a959dde675b8.jpg"
},
"1067407": {
"id": "1067407",
"url": "https://cdn.gowadogo.com/180af6a5-251d-4aa9-9cd9-51b2fc77d0c6.jpg"
}
}
myjson["Offer"]= {
"advanced_targeting_enabled": "f",
"category_name": "E-commerce/ Shopping",
"click_lifespan": "168",
"conversion_cap": "50",
"currency": "USD",
"default_payout": "1.5"
}
json_data = json.dumps(myjson)
#reverse back into a json
paths=[]
def walk_the_tree(inputDict,suffix=None):
for key, value in inputDict.items():
if isinstance(value, dict):
if suffix==None:
suffix=key
else:
suffix+=":"+key
walk_the_tree(value,suffix)
else:
paths.append(suffix+":"+key+":"+value)
walk_the_tree(myjson)
print(paths)
#split and build your nested dictionary
json_specs = {}
for path in paths:
parts=path.split(':')
value=(parts[-1])
d=json_specs
for p in parts[:-1]:
if p==parts[-2]:
d = d.setdefault(p,value)
else:
d = d.setdefault(p,{})
print(json_specs)
Paths:
['Country:KR:id:220', 'Country:KR:name:South Korea', 'Country:Creative:1067405:id:1067405', 'Country:Creative:1067405:url:https://cdn.gowadogo.com/559d1ba1-8d50-4c7f-b3f5-d80f918006e0.jpg', 'Country:Creative:1067405:1067406:id:1067406', 'Country:Creative:1067405:1067406:url:https://cdn.gowadogo.com/3799a70d-339c-4ecb-bc1f-a959dde675b8.jpg', 'Country:Creative:1067405:1067406:1067407:id:1067407', 'Country:Creative:1067405:1067406:1067407:url:https://cdn.gowadogo.com/180af6a5-251d-4aa9-9cd9-51b2fc77d0c6.jpg', 'Country:Creative:Offer:advanced_targeting_enabled:f', 'Country:Creative:Offer:category_name:E-commerce/ Shopping', 'Country:Creative:Offer:click_lifespan:168', 'Country:Creative:Offer:conversion_cap:50', 'Country:Creative:Offer:currency:USD', 'Country:Creative:Offer:default_payout:1.5']
json_data['key'] = 'value'
,但这不起作用,因为json_data
不是Python字典。 - Martijn Pieterspip list
可以给你相同的信息,并且可以输出 JSON 格式:pip list --format=json
。 - Martijn Pieters