我从目标C的关键值得到了价值,但是在swift中我不知道怎么做?
NSDictionary *dictResult = [NSJSONSerialization
JSONObjectWithData:jsonData options:0 error:&error];
if ([dictResult objectForKey:@"wsResponse"]!=nil)
{
for (NSDictionary *dict in [[dictResult valueForKey:@"wsResponse"]valueForKey:@"Bhaktamar"])
{
objBean=[[Beandata alloc]init];
objBean.strcontent=[dict objectForKey:@"content"];
objBean.strtitle=[dict objectForKey:@"title"];
[dataBase insertJsonData:@"BhaktamarData" Title:objBean.strtitle Content:objBean.strcontent];
}
}
先谢谢
JSON ARRAY
{"wsResponse":{"Bhaktamar":[{"content":"atipati nath","title":"atipatinath"},{"content":"atipati nath","title":"atipatinath"}]}
答案 0 :(得分:0)
Swift 3的等价物是:
do {
if let dictResult = try JSONSerialization.jsonObject(with:jsonData, options:[]) as? [String:Any] {
if let wsResponse = dictResult["wsResponse"] as? [String:Any], let shlock = wsResponse["Bhaktamar"] as? [[String:Any]] {
for dict in shlock {
objBean = Beandata()
objBean.strcontent = dict["content"] as? String ?? ""
objBean.strtitle = dict["title"] as? String ?? ""
dataBase.insertJsonData("BhaktamarData", Title:objBean.strtitle, Content:objBean.strcontent)
}
}
}
} catch {
print(error)
}
插入行的语法可能不同(第一个参数必须有参数标签)。
PS:考虑变量objBean
在重复循环中初始化但实际上未使用。