我有一个json data,它提供以下信息:
let data = [
{
"QuestionTitle" : "Entomology is the science that studies",
"Id" : 205,
"Options" : [
{ "Option" : "Insects", "Id" : 810 },
{ "Option" : "The origin and history of technical and scientific terms", "Id" : 811 },
{ "Option" : "The formation of rocks", "Id" : 812 },
{ "Option" : "Behavior of human beings", "Id" : 809 }
]
},
{
"QuestionTitle" : "A train running at the speed of …Run Code Online (Sandbox Code Playgroud) 我有一系列类型的字典
[["OptionId": 824, "QuestionId": 208],
["OptionId": 810, "QuestionId": 205],
["OptionId": 1017, "QuestionId": 257],
["OptionId": 0, "QuestionId": 201],
["OptionId": 0, "QuestionId": 199],
["OptionId": 0, "QuestionId": 200]]
Run Code Online (Sandbox Code Playgroud)
我迭代了这些值并将字典值提取为
["OptionId": 824, "QuestionId": 208]
["OptionId": 810, "QuestionId": 205]
["OptionId": 1017, "QuestionId": 257]
["OptionId": 0, "QuestionId": 201]
["OptionId": 0, "QuestionId": 199]
["OptionId": 0, "QuestionId": 200]
Run Code Online (Sandbox Code Playgroud)
现在,我想获取所有为 0 的“OptionId”的“QuestionId”。如何将字典键值与零进行比较?提前致谢。
这是我到目前为止所尝试过的:
for dictionary in arrayofDict {
print(dictionary)
if (dictionary["OptionId"] == 0) {
print("option not selected")
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个称为字典的数组,arrayOfDict其中包含许多都具有相同键的字典对象:QUESTIONID和OPTIONID。
[ //arrayOfDict
{QUESTIONID:1, OPTIONID:0},
{QUESTIONID:2, OPTIONID:201},
{QUESTIONID:3, OPTIONID:204)
];
Run Code Online (Sandbox Code Playgroud)
arrayOfDict仅当我要添加的字典不包含此类字典时,才需要添加此类字典QUESTIONID。如果QUESTIONID已经存在,则OPTIONID需要用新的替换。如何QUESTIONID与QUESTIONIDin 比较arrayOfDict?