JSON Parsing in iOS 7

前端 未结 14 2244
长情又很酷
长情又很酷 2020-12-04 11:25

I am creating an app for as existing website. They currently has the JSON in the following format :

[

   {
       \"id\": \"value\",
       \"array\": \"[{\         


        
14条回答
  •  -上瘾入骨i
    2020-12-04 12:12

    // ----------------- json for localfile---------------------------

    NSString *pathofjson = [[NSBundle mainBundle]pathForResource:@"test1" ofType:@"json"];
    NSData *dataforjson = [[NSData alloc]initWithContentsOfFile:pathofjson];
    arrayforjson = [NSJSONSerialization JSONObjectWithData:dataforjson options:NSJSONReadingMutableContainers error:nil];
    [tableview reloadData];
    

    //------------- json for urlfile-----------------------------------

    NSString *urlstrng = @"http://www.json-generator.com/api/json/get/ctILPMfuPS?indent=4";
    NSURL *urlname = [NSURL URLWithString:urlstrng];
    NSURLRequest *rqsturl = [NSURLRequest requestWithURL:urlname];
    

    //------------ json for urlfile by asynchronous----------------------

    [NSURLConnection sendAsynchronousRequest:rqsturl queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        arrayforjson = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        [tableview reloadData];
    }];
    

    //------------- json for urlfile by synchronous----------------------

    NSError *error;
    NSData *data = [NSURLConnection sendSynchronousRequest:rqsturl returningResponse:nil error:&error];
    
     arrayforjson = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    
    [tableview reloadData];
    } ;
    

提交回复
热议问题