我的字典如下所示:
{
"permutations": [
{
"testname": "test1",
"file_type": "text",
"app_parent_folder": "folder",
"expected_result": "block"
},
{
"testname": "test2",
"file_type": "text",
"app_parent_folder": "folder",
"expected_result": "block"
}
]
}
我想添加一个新对象" rule_id" :(测试用例的数量),以便修改后的json如下所示
{
"permutations": [
{
"testname": "test1",
"file_type": "text",
"app_parent_folder": "folder",
"expected_result": "block",
"rule_id": "1"
},
{
"testname": "test2",
"file_type": "text",
"app_parent_folder": "folder",
"expected_result": "block",
"rule_id": "2"
}
]
}
我尝试使用以下代码但未获得所需结果
import json
from pprint import pprint
with open('example.json') as data_file:
data = json.load(data_file)
for rule_id_num in range(1,2):
data["permutations"].append('rule_id':rule_id_num)
pprint(data)
有人可以帮我解决如何在上面的json文件中添加字典的问题。提前谢谢!!
答案 0 :(得分:3)
由于您要添加rule_id的容器是字典,因此无法附加到它们。您必须按键添加值。
试试这个:
class func saveToCameraRoll(imageUrl: String) {
ImageService.GetImageData(url: imageUrl) { data in // This helper function just fetches Data from the url using Alamofire
guard let data = data else { return }
PHPhotoLibrary.shared().performChanges({ _ in
PHAssetCreationRequest.forAsset().addResource(with: .photo, data: data, options: nil)
}) { success, error in
guard success else {
log.debug("failed to save gif \(error)")
return
}
log.debug("successfully saved gif")
}
}
}
我要问的一点是,testname和rule_id之间是否存在某种关系?