继续检索数据数组中的第一个元素

时间:2014-04-23 09:25:12

标签: ios objective-c json

我一直在从数组中检索第一个元素,我在数组中有13个项目,但是每次遍历数组时它都会拉出第一个元素并且不会增加。

如何让我为数组中的每个元素增加数据并从数组中提取数据?

然后是JSON:

[
   {
      "CategoryId":"1",
      "Name":"education"
   },
   {
      "CategoryId":"2",
      "Name":"allotments\r"
   },
   {
      "CategoryId":"3",
      "Name":"anti social behaviour\r"
   },
   {
      "CategoryId":"4",
      "Name":"police\r"
   },
   {
      "CategoryId":"5",
      "Name":"community \r"
   },
   {
      "CategoryId":"6",
      "Name":"council housing\r"
   },
   {
      "CategoryId":"7",
      "Name":"council\r"
   },
   {
      "CategoryId":"8",
      "Name":"doctors\r"
   },
   {
      "CategoryId":"9",
      "Name":"business\r"
   },
   {
      "CategoryId":"10",
      "Name":"disability \r"
   },
   {
      "CategoryId":"11",
      "Name":"employment\r"
   },
   {
      "CategoryId":"12",
      "Name":"money matters\r"
   },
   {
      "CategoryId":"13",
      "Name":"recreation"
   }
]

代码:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: @"http://localhost:8888/MAMP/WHFC/categories.php"]];

NSError *e;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&e];
NSString *strResult = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

//NSLog(@"%@", strResult);
NSArray *notifications = [data JSONValue];

int i = 0;
for (id obj in notifications){
    id myArrayElement = [notifications objectAtIndex:i];
    NSString *CategoryId = [myArrayElement objectForKey:@"CategoryId"];
    NSString *Name = [myArrayElement objectForKey:@"Name"];
    if(NULL){

    }
    i = i + 1;
}

3 个答案:

答案 0 :(得分:0)

    Sending **asynchronousRequest** will not block your main thread and ur UI Remain responsive. 


 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: @"http://localhost:8888/MAMP/WHFC/categories.php"]];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error1)
     {
         if (error1==nil && [data length]>0)
         {
             NSError* error;
             id jsonObject=[NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
             if (error==nil)
             {
                 if ([jsonObject isKindOfClass:[NSDictionary class]])
                 {
                     //handle dict here
                 }
                 else if ([jsonObject isKindOfClass:[NSArray class]])
                 {
                     NSArray* arrDict=jsonObject;
                     for (NSDictionary* dict in arrDict)
                     {
                         NSString *CategoryId = [dict objectForKey:@"CategoryId"];
                         NSString *Name = [dict objectForKey:@"Name"];
                         NSLog(@"Name= %@",Name);
                         NSLog(@"CategoryID = %@",CategoryId);
                     }
                 }
             }
         }
         else
         {
             NSLog(@"Something bad happened");
         }
     }];

答案 1 :(得分:0)

使用NSJSONSerialization

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: @"http://localhost:8888/MAMP/WHFC/categories.php"]];

NSError *e;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&e];
NSArray *arrCategoryList = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&parsedError];

现在iterate

for(NSDictionary *dictCategory in arrCategoryList)
{
   NSString *strCategoryId = [dictCategory objectForKey:@"CategoryId"];
   NSString *strCategoryName = [dictCategory objectForKey:@"Name"];
   NSLog(@"%@ : %@",strCategoryId,strCategoryName);
}

答案 2 :(得分:0)

试试这个:

NSURL *s = [NSURL URLWithString: @"http://localhost:8888/MAMP/WHFC/categories.php"]];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:[self getAbsoluteURL:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.00];
NSHTTPURLResponse *response;
NSError *error = [[NSError alloc]init];
NSData *apiData = [NSURLConnection sendSynchronousRequest:requestURL returningResponse:&response error:&error];
NSArray *arr = [NSJSONSerialization JSONObjectWithData:apiData options:kNilOptions error:&error];

您从JSON获得NSArry。之后解析这样做:

for(NSDictionary *dictionary in apiData) {
    NSString *cat = [obj objectForKey:@"CategoryId"];
    NSString *name = [obj objectForKey:@"Name"];
}

希望这有助于......:)

相关问题