我对IOS开发真的很陌生。我正在开发一个从Web服务获取数据的应用程序。我正在使用一种方法来调用服务并获得响应。实际上我把我的回复作为一个字符串。但我把它转换成NsData类型变量。我引用这段代码
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
NSArray* latestLoans = [json objectForKey:@"loans"]; //2
NSDictionary* loan = [latestLoans objectAtIndex:0];
NSNumber* fundedAmount = [loan objectForKey:@"funded_amount"];
NSNumber* loanAmount = [loan objectForKey:@"loan_amount"];
float outstandingAmount = [loanAmount floatValue] -
[fundedAmount floatValue];
humanReadble.text = [NSString stringWithFormat:@"Latest loan: %@
from %@ needs another $%.2f to pursue their entrepreneural dream",
[loan objectForKey:@"name"],
[(NSDictionary*)[loan objectForKey:@"location"]
objectForKey:@"country"],
outstandingAmount];
但在我的情况下,我的代码段在第3行给出了一个错误...... 是NSDictionary * loan = [latestLoans objectAtIndex:0];
错误是:[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0xa959a60
我对网络服务的回应是
{
"d": {
"__type": "Paragonsoft.Caregiver.WebUI.Public.Mobile.DTO.ProfileDTO",
"PersonId": 58,
"PersonTypeId": 2,
"FirstName": "Aruna",
"LastName": "Wanniarrchi",
"Email": "sudheeraxcvb@paragonsoftlog.net",
"Title": 0,
"Gender": 1,
"Phone": "(343) 123-4324",
"MobileNo": "(423) 123-4234",
"ImageURL": " ../images/UploadsTest/ProfileImagesLarge/CG/16/2172014220495482.jpg",
"Age": 38,
"MaritalStatus": null,
"HourlyRateFrom": 25,
"HourlyRateTo": 35,
"CaringHours": null,
"Schedules": "Reliever, On Call, Regular",
"Languages": "English, Cantonese, Other",
"MedicalEquips": "Hoyer Lift, Walker, Wheelchair",
"Street": "Test Streetaa",
"City": "Los Angeles",
"Code": "CA",
"ZipCode": "90078"
}
}
以下是我的代码
NSString* user = self.userName.text;
NSString* password = self.passWord.text;
NSArray* values = [NSArray arrayWithObjects:user,password, nil];
NSArray* keys = [NSArray arrayWithObjects:@"username",@"password", nil];
NSString* urls = [NSString stringWithFormat:@"myUrl"];
jsonpaser *jp = [[jsonpaser alloc]init];
NSString* result = [jp getjsonFromURltest:urls :keys :values];
//convert the result string to nsData
NSData* result_data = [result dataUsingEncoding:NSUTF8StringEncoding];
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:result_data options:kNilOptions error:&error];
NSArray* jsonrespond = [json objectForKey:@"d"]; //2
NSLog(@"Result: %@", jsonrespond);
NSDictionary* userDetails = [jsonrespond objectAtIndex:0]; // this is the point i got error
// i want to read some specific item from my response
NSString* userId = [userDetails objectForKey:@"PersonId"];
我想我将字典作为一个数组...但我不知道如何修复它。
答案 0 :(得分:0)
“贷款”字段在网络服务中不可用,因此您无法访问它...
NSArray* latestLoans = [json objectForKey:@"loans"]
而不是......
NSArray* latestLoans = [json objectForKey:@"__type"];
你明白了......,“_ _ type”是网络服务的领域,所以你可以得到它......
答案 1 :(得分:0)
您正在尝试将字典数据作为数组获取。试试这个
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:result_data
options:kNilOptions
error:&error];
NSDictionary* jsonrespond = [json objectForKey:@"d"]; //2
NSLog(@"Result: %@", jsonrespond);
if (jsonrespond) {
NSString* userId = [jsonrespond objectForKey:@"PersonId"];
}
您在NSDictionary* loan = [latestLoans objectAtIndex:0];
收到错误,因为您正在尝试使用字典中的objectAtIndex
获取元素。由于latestLoans是字典,它会引发错误。它不是一个数组。