Как мне создать вложенный объект JSON с помощью Python? - PullRequest
0 голосов
/ 11 сентября 2018

У меня есть следующий код:

data = {}
data['agentid'] = 'john'
data['eventType'] = 'view'
json_data = json.dumps(data)

print json_date = {"eventType": "view," agentid ":" john "}

Я хочу создать вложенныйОбъект JSON, например: *

{
    "agent": { "agentid", "john"} ,
    "content": {
        "eventType": "view",
        "othervar": "new"
    }
}

Как мне это сделать? Я использую Python 2.7.

Приветствия Ник

Ответы [ 2 ]

0 голосов
/ 18 мая 2019

Тип вложенного массива:

enter image description here

выход: * +1010 *

{"collection": [{"website": "stackabuse.com", "from": "Небраска", "name": "Scott"}, {"from2": {"nestedcollection": [{" website ":" apple.com "," from ":" Alabama "," name ":" Tim "}, {" website ":" google.com "," from ":" Alabama "," name ":" Майк "}]}," name2 ":" группа вложения "," website2 ":" группа вложения "}]}

0 голосов
/ 11 сентября 2018

Вы можете вложить словари следующим образом:

jsondata = {}
agent={}
content={}
agent['agentid'] = 'john'
content['eventType'] = 'view'
content['othervar'] = "new"

jsondata['agent'] = agent
jsondata['content'] = content
print(json.dumps(jsondata))

Вывод: print {"content": {"eventType": "view", "othervar": "new"}, "agent":{"agentid": "john"}}

...