How to assign values from NSMutableDictionary to NSArray

折月煮酒 提交于 2019-11-29 18:15:55

Try editing fourth line in connectionDidFinishLoading to

values = [responseString JSONFragments];
NSError *error = nil;
NSArray *array = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
NSLog(@"Your data - %@",array);

Now you can get it according to data format.

EDIT

I think you also dont know how to get a webResponse.
So here is a way to get webResponse - First set XML delegate in your ViewController.h class and declare a NSMutableData globaly

@interface ViewController : UIViewController<NSXMLParserDelegate>
@property(nonatomic, retain)NSMutableData  *responseData;
@end

Now synthesized this responseData in your ViewController.m class

@synthesize responseData = _responseData;

Now you can send request on server in viewDidLoad: method its up to you in which method you want to send it.

-(void)viewDidLoad
{

    NSString *urlString = [NSString stringWithFormat:@"http://EnterYourURLHere"];

    NSURL *URL = [NSURL URLWithString:urlString];
    NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]init];
    [urlRequest setURL:URL];
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];

    NSURLConnection *urlConnection = [[NSURLConnection alloc]initWithRequest:urlRequest delegate:self];
    if(!urlConnection)
    {
        [[[UIAlertView alloc]initWithTitle:@"OOoopppssS !!" message:@"There is an error occured. Please check your internet connection or try again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
    }

}

#pragma mark - Parsing delegate methods

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.responseData = [[NSMutableData alloc]init];
    [self.responseData setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.responseData appendData:data];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //Now parse your data here -
    NSError *error = nil;
    NSArray *array = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableContainers error:&error];
    NSLog(@"Your data - %@",array);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!