修改位于 JSON DICTIONARY 中的 LIST 内的 VALUE
Modify VALUE inside LIST located in JSON DICTIONARY
我想问一下这是我的JSON文件。如果我想将我的 'c' 更改为 '9',我该如何写入数据?
{
'x': 4,
'y': 5,
'z': [
{
'a': 1,
'b': 2,
'c': 3
}
]
}
import urllib.request
import json
data = ?????
myurl = "http://192.168.1.10:8888/testJSON"
req = urllib.request.Request(myurl)
req.add_header('Content-Type', 'application/json; charset=utf-8')
jsondata = json.dumps(data)
jsondataasbytes = jsondata.encode('utf-8') # needs to be bytes
req.add_header('Content-Length', len(jsondataasbytes))
response = urllib.request.urlopen(req, jsondataasbytes)
@BhagyeshDudhediya 提供的解决方案有效:
import urllib.request
import json
data['z'][0]['c'] = 9
myurl = "http://192.168.1.10:8888/testJSON"
req = urllib.request.Request(myurl)
req.add_header('Content-Type', 'application/json; charset=utf-8')
jsondata = json.dumps(data)
jsondataasbytes = jsondata.encode('utf-8') # needs to be bytes
req.add_header('Content-Length', len(jsondataasbytes))
response = urllib.request.urlopen(req, jsondataasbytes)
只需这样做:data['z'][0]['c'] = 9
我想问一下这是我的JSON文件。如果我想将我的 'c' 更改为 '9',我该如何写入数据?
{
'x': 4,
'y': 5,
'z': [
{
'a': 1,
'b': 2,
'c': 3
}
]
}
import urllib.request
import json
data = ?????
myurl = "http://192.168.1.10:8888/testJSON"
req = urllib.request.Request(myurl)
req.add_header('Content-Type', 'application/json; charset=utf-8')
jsondata = json.dumps(data)
jsondataasbytes = jsondata.encode('utf-8') # needs to be bytes
req.add_header('Content-Length', len(jsondataasbytes))
response = urllib.request.urlopen(req, jsondataasbytes)
@BhagyeshDudhediya 提供的解决方案有效:
import urllib.request
import json
data['z'][0]['c'] = 9
myurl = "http://192.168.1.10:8888/testJSON"
req = urllib.request.Request(myurl)
req.add_header('Content-Type', 'application/json; charset=utf-8')
jsondata = json.dumps(data)
jsondataasbytes = jsondata.encode('utf-8') # needs to be bytes
req.add_header('Content-Length', len(jsondataasbytes))
response = urllib.request.urlopen(req, jsondataasbytes)
只需这样做:data['z'][0]['c'] = 9