I get value from key in objective C but in swift i don't know how to do it?
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];
}
}
Thanks in Advance
JSON ARRAY
{"wsResponse":{"Bhaktamar":[{"content":"atipati nath","title":"atipatinath"},{"content":"atipati nath","title":"atipatinath"}]}
The Swift 3 equivalent is:
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)
}
The syntax for the insert line is probably different (there must be a parameter label for the first parameter).
PS: Consider that the variable objBean
is initialized in the repeat loop but actually unused.