我是Alamofire在我的项目中进行API调用,我想以下面的格式为“语言”键创建参数:
[
{
"isSelected": true,
"languageProficiencies": [
{
"isSelected": false,
"name": "Advance"
},
{
"isSelected": true,
"name": "Proficient"
},
{
"isSelected": false,
"name": "Basic"
},
{
"isSelected": false,
"name": "Below Basic"
}
],
"name": "English"
},
{
"isSelected": false,
"languageProficiencies": [
{
"isSelected": false,
"name": "Advance"
},
{
"isSelected": false,
"name": "Proficient"
},
{
"isSelected": false,
"name": "Basic"
},
{
"isSelected": false,
"name": "Below Basic"
}
],
"name": "Malay"
},
{
"isSelected": false,
"languageProficiencies": [
{
"isSelected": false,
"name": "Advance"
},
{
"isSelected": false,
"name": "Proficient"
},
{
"isSelected": false,
"name": "Basic"
},
{
"isSelected": false,
"name": "Below Basic"
}
],
"name": "Mandarin"
},
{
"isSelected": false,
"languageProficiencies": [
{
"isSelected": false,
"name": "Advance"
},
{
"isSelected": false,
"name": "Proficient"
},
{
"isSelected": false,
"name": "Basic"
},
{
"isSelected": false,
"name": "Below Basic"
}
],
"name": "Tamil"
}
]
目前我的“语言”键格式为:
["resource": ["language": [["languageProficiencies": [["name": "Advance", "isSelected": true], ["name": "Proficient", "isSelected": false], ["name": "Basic", "isSelected": false], ["name": "Below Basic", "isSelected": false]], "isSelected": "true", "name": "English"], ["languageProficiencies": [["name": "Advance", "isSelected": false], ["name": "Proficient", "isSelected": false], ["name": "Basic", "isSelected": false], ["name": "Below Basic", "isSelected": false]], "isSelected": "false", "name": "Malay"], ["languageProficiencies": [["name": "Advance", "isSelected": false], ["name": "Proficient", "isSelected": false], ["name": "Basic", "isSelected": false], ["name": "Below Basic", "isSelected": false]], "isSelected": "false", "name": "Mandarin"], ["languageProficiencies": [["name": "Advance", "isSelected": false], ["name": "Proficient", "isSelected": false], ["name": "Basic", "isSelected": false], ["name": "Below Basic", "isSelected": false]], "isSelected": "false", "name": "Tamil"]], "user_id": 384]]
请提供建议,使其与我先提到的格式相同。提前致谢!!快乐的编码!!
答案 0 :(得分:1)
Array包含字典和languageProficiencies的对象也是一个数组,它还包含字典对象。
您可以使用以下代码存档此类格式:
var mainArray:[[String: Any]] = []
for i in 0...1 {
var dict:[String: Any] = [:]
dict["isSelected"] = "true"
dict["name"] = "English"
var arrLanguageProficiencies:[[String: Any]] = []
for j in 0...1 {
var dictLangProf:[String: Any] = [:]
dictLangProf["isSelected"] = "false"
dictLangProf["name"] = "Advance"
arrLanguageProficiencies.append(dictLangProf)
}
dict["languageProficiencies"] = arrLanguageProficiencies
mainArray.append(dict)
}
do {
let jsonData = try JSONSerialization.data(withJSONObject: mainArray, options: JSONSerialization.WritingOptions.prettyPrinted)
let theJSONText = NSString(data: jsonData, encoding: String.Encoding.ascii.rawValue)!
print ("JSON Text :"+(theJSONText as String))
} catch let error as NSError {
print(error)
}
输出看起来像:
[
{
"name" : "English",
"isSelected" : "true",
"languageProficiencies" : [
{
"name" : "Advance",
"isSelected" : "false"
},
{
"name" : "Advance",
"isSelected" : "false"
}
]
},
{
"name" : "English",
"isSelected" : "true",
"languageProficiencies" : [
{
"name" : "Advance",
"isSelected" : "false"
},
{
"name" : "Advance",
"isSelected" : "false"
}
]
}
]