我如何迭代以下json文件,如果fa ="cc.ee"然后在fb中添加一个值?
{
"pk": 1,
"fa": "cc.ee",
"fb": {
"fc": "",
"fd_id": "12345",
}
},
#!/usr/bin/env python
import json,urllib
json_data=open("my.json")
data = json.load(json_data)
for entry in data:
json.dumps(entry)
json_data.close()
exit
Run Code Online (Sandbox Code Playgroud)
Pab*_*blo 17
JSON对象的行为类似于字典.您可以像分配字典一样分配新密钥来添加值:
json_string = """
{
"pk": 1,
"fa": "cc.ee",
"fb": {
"fc": "",
"fd_id": "12345"
}
}"""
import json
data = json.loads(json_string)
if data["fa"] == "cc.ee":
data["fb"]["new_key"] = "cc.ee was present!"
print json.dumps(data)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19438 次 |
| 最近记录: |