Hi I am a beginner in ios and I am trying to get below loans array inside "id" details.
And I would like a store that details in separate array, I have tried using some code but I did not get any results.
Someone help me please.
my code:
#import "ViewController.h"
#import "MBProgressHUD.h"
@interface ViewController ()
{
NSMutableData * webData;
NSURLConnection * connection;
NSMutableArray * array;
}
@end
@implementation ViewController
- (void)viewDidLoad {
array = [[NSMutableArray alloc]init];
NSURL * url = [NSURL URLWithString:@"http://192.168.2.104:8080/RouteTracking/maprun/Route/GetRouteListByUserId?userId=511187762371709"];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
connection = [NSURLConnection connectionWithRequest:request delegate:self];
if(connection)
{
webData = [[NSMutableData alloc]init];
}
[super viewDidLoad];
// [MBProgressHUD showHUDAddedTo:self.view animated:YES];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[webData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[webData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"error is %@",[error localizedDescription]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString * allDataDictionbary = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
NSDictionary * responseString = [allDataDictionbary JSONValue];
NSArray * allTweets = [responseString objectForKey:@"routeDetails"];
NSLog(@"details are %@",allTweets);
for (NSDictionary * obj in allTweets) {
NSDictionary * act = [obj objectForKey:@"loans"];
NSLog(@"main dictionary is %@",act);
NSString * name = [act objectForKey:@"id"];
[array addObject:name];
}
NSLog(@"act array is %@",array);
}
Here's the setup of my JSON:
routeDetails
[
-{
id: 285,
name: name 1,
-loans: [
-{
id: 42,
name: "Shark"
}
]
},
-{
id: 286,
name: name 2,
-loans: [
-{
id: 50,
name: "Flipper"
}
]
}
]