我有JSON response.body
{"0":
{"id":724,
"firstName":"Dr. Anna-Maria",
"lastName":"Boehler (Bohler)",
"city":"Luxembourg"},
"1":
{"id":1542,
"firstName":"Joanna",
"lastName":"Kotlewska (Biever)",
"city":"Rodange"},
"2":
{"id":1681,
"firstName":"Dr. Joanna",
"lastName":"Louwagie (Hoche)",
"city":"Luxembourg"},
"3":
{"id":2161,"firstName":"Magdalena",
"lastName":"Sarrazin (Owsianna)",
"city":"Luxembourg"},
"4":
{"id":10822,"firstName":"Anna",
"lastName":"Kogut-Aramini",
"city":"Diekirch"}
,"uri":"search-by-name",
"paramsURL":{"country":"1","name":"Anna"}}
AppDelegate中的添加RKObjectMapping和RKDynamicMapping
RKObjectMapping *showListDoctorMapping = [RKObjectMapping mappingForClass:[ShowListResultResponse class]];
[showListDoctorMapping addAttributeMappingsFromDictionary:@{
@"id" : @"id",
@"firstName" : @"firstName",
@"lastName" : @"lastName",
@"city" : @"city"
}];
RKDynamicMapping *dynamicMapping = [RKDynamicMapping new];
[dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) {
NSDictionary *dict = representation;
NSArray *keys=[dict allKeys];
RKObjectMapping *dataMapping =[RKObjectMapping mappingForClass:[ShowListResultResponse class]];
for (NSString *key in keys){
[dataMapping addRelationshipMappingWithSourceKeyPath:key mapping:showListDoctorMapping ];
}
return dataMapping;
}];
ShowRensponse
并添加ShowListResultResponse此类Mapping
import UIKit
import Foundation
class ShowListResultResponse: NSObject {
var id: Int?
var fristName: String?
var lastName: String?
var city: String?
override init() {
super.init()
}
init(id: Int, fistName: String, lastName: String, city: String) {
self.id = id
self.fristName = fistName
self.lastName = lastName
self.city = city
}
}
类DoctorsDataSource
import UIKit
class DoctorsDataSource: NSObject, MLPAutoCompleteTextFieldDataSource {
var listDoctors=[String]()
func autoCompleteTextField(textField: MLPAutoCompleteTextField!, possibleCompletionsForString string: String!, completionHandler handler: ([AnyObject]!) -> ()) {
let appDelegate: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
var requestParameters: [NSObject:AnyObject] = [NSObject: AnyObject]()
requestParameters["name"] = textField.text
requestParameters["country"]="1"
// Pobiera długość znaku textField
let lenght = textField.text!.characters.count
if lenght>3{
let request = appDelegate.objectManager.requestWithObject(nil, method: .GET, path: "/search-by-name", parameters: requestParameters)
request.setValue("application/json", forHTTPHeaderField: "Accept")
let operation = appDelegate.objectManager.objectRequestOperationWithRequest(request, success: { (operation: RKObjectRequestOperation!, rkmap: RKMappingResult!) in
let list = rkmap.firstObject as AnyObject
if let doctors = list as? [ShowListResultResponse]{
//if let doctors = rkmap.array() as? [ShowListResultResponse] {
for doctor: ShowListResultResponse in doctors {
self.listDoctors.append("\(doctor.fristName) \(doctor.lastName)")
}
}
handler(self.listDoctors);
}) { (operation: RKObjectRequestOperation!, error: NSError!) in
print("Load filed with error: @", error!)
self.performSelectorOnMainThread(#selector(AppDelegate.showFetchError), withObject: nil, waitUntilDone: true)
}
appDelegate.objectManager.enqueueObjectRequestOperation(operation)
// }
}}
}
我不知道为什么不显示医生名单
答案 0 :(得分:0)
如果我不熟悉那个第三方 但是
JSON响应是一个包含键(字符串)和值(字典)的字典,您尝试将其作为数组读取
如果让医生=列表为? [ShowListResultResponse] 强>
尝试将其作为字典阅读
if let dict = list as? [String:ShowListResultResponse]{
let doctors = Array(dict.values)
}
您也可能想查看rkmap内容