Reputation: 326
I am new in Handling Response. While trying to fetch Values("title") in array, it is not working properly. It is updating Last Value alone in Array. How can I fetch all values("title") in an array? I even tried changing lastObject
as objectAtIndex:
Below is my Response Output and Code.
NSDictionary *book = [[ jsonResults objectForKey:@"result"] lastObject];
NSArray *title = (NSArray *) [book objectForKey:@"title"];
Response Output:
{
"result":
[
{
"authors": [
"AuthRick"
],
"bno": 9280621999,
"title": "Foo",
"urlImage": "www.foo.hoo",
},{
"authors": [
"MaveRick"
],
"bno": 9951621988,
"title": "Hoo",
"urlImage": "www.foo.hoo",
}
]
}
Upvotes: 0
Views: 70
Reputation: 5186
You can get all the title field value in title array with below code :
NSArray *resultArray = [jsonResults objectForKey:@"result"];
NSMutableArray *title = [NSMutableArray alloc] init];
for(NSDictionary *obj in resultArray)
{
[arytitle addObject:[obj valueForKey:@"title"]];
}
Upvotes: 1
Reputation: 398
try this..
NSDictionary *response; //getting your response as Dictionary,so keep response dictionary in response variable(i.e response dictionary)
NSArray *arr =[response valueForKey:@"result"]; //get result Array
for (int i=0; i<arr.count; i++) {
NSLog(@"title--%@",[[arr objectAtIndex:i] valueForKey:@"title"]); //here your getting two titles of data
}
Upvotes: 1
Reputation: 859
when i look at this, this may suits this response,
NSMutableArray *titleArray = [[NSMutableArray alloc]init];
NSArray *resultArray = [ jsonResults objectForKey:@"result"];
for(NSDictionary *individualBookDictionary in resultArray)
{
[titleArray addObject:[individualDictionary valueForKey:@"title"]];
}
Upvotes: 1