简体   繁体   中英

Manipulating Values Python Dictionary (JSON Object)

I'm using requests in python for dealing with sending data (Post + JSON)

Here is my problem:

payload = {"credentials": {"deviceid": "XXX","pin": "XXX"},"data": [{"id": "","name": "","type": "","data": [{"value": "","date": ""}]}]}

Then I want to fill the blank values ("" not XXX) by variables in Python

payload["id"] = payload_id
payload["name"] = payload_name
payload["type"] = payload_type
payload["value"] = payload_value
payload["date"] = payload_datetime

If I print now

payload["id"]

everything works.

But if I print the whole

print payload

Then the value is not changed. But the value has been added. Than I have two of each. One empty and one with the correct value.

id键是词典的一部分,该词典是列表的成员,该列表包含您必须使用数据键访问的另一个词典。

payload["data"][0]["id"]

You should go down to the correct level ie payload["data"][0]["id"] instead of payload["id"]

Thus, it should be :

payload["data"][0]["id"] = payload_id
payload["data"][0]["name"] = payload_name
payload["data"][0]["type"] = payload_type
payload["data"][0]["data"][0]["value"] = payload_value
payload["data"][0]["data"][0]["date"] = payload_datetime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM