Question

im really new to IOS development. i am developing an application which is getting data from a web service. i am using a method which will call the service and get the response. actually im getting my response as a string. but im converted it into NsData type variable. im referring this code

 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];

but in my case, my code segment gives me an error in the 3rd line... which is NSDictionary* loan = [latestLoans objectAtIndex:0];

Error is: [__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0xa959a60

my response from the web service is this

{
  "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"
  }
}

Below is my code

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"];

I guess I'm getting dictionary as a array..but i dont know how to fix it.

Was it helpful?

Solution

You are trying to fetch the dictionary data as array. Try this

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"];
}

you are getting error in NSDictionary* loan = [latestLoans objectAtIndex:0]; because you are trying get elements using objectAtIndex from dictionary. Since the latestLoans is dictionary it throws you error. Its not an array.

OTHER TIPS

the "loans" field is not available in web service, so you cant access it...

NSArray* latestLoans = [json objectForKey:@"loans"]

instead of.....

NSArray* latestLoans = [json objectForKey:@"__type"];

You get it.., The "__type" is the field of web service so you can get it...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top