如何使用web url解析ios中的json

时间:2013-12-16 10:13:47

标签: ios json

我从外部源获取json并在ios中解析它。我的代码在下面

注意= json和cats变量是NSArray;

    NSURL * url = [NSURL URLWithString:@"http://myurl.json"];
    NSData * data = [NSData dataWithContentsOfURL:url];

    json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

    cats = [[NSMutableArray alloc] init];

    for (int i=0; i < json.count; i++) {
        NSString * CatId = [[json objectAtIndex:i] objectForKey:@"id"];
        NSString * CatName = [[json objectAtIndex:i] objectForKey:@"name"];
        NSString * CatIcon = [[json objectAtIndex:i] objectForKey:@"icon"];
        categories * cat = [[categories alloc] initWithCId:CatId andCName:CatName andCIcon:CatIcon];

        [cats addObject:cat];

和json在这里

{"categories":[{"id":1,"name":"Healthcare","icon":"/icons/images/65/original_56.png?1386745569"},{"id":10,"name":"Mall","icon":"/icons/images/60/original_51.png?1386745369"},{"id":11,"name":"Taupheq","icon":"/icons/images/23/original_14.png?1386744595"},{"id":12,"name":"Hotel","icon":"/icons/images/27/original_18.png?1386744659"},{"id":13,"name":"SPA","icon":"/icons/images/48/original_39.png?1386745093"},{"id":14,"name":"ATM","icon":"/icons/images/22/original_13.png?1386744578"},{"id":15,"name":"Travel","icon":"/icons/images/12/original_3.png?1386744393"},{"id":16,"name":"Game zone","icon":"/icons/images/68/original_59.png?1386745626"},{"id":17,"name":"Academic","icon":"/icons/images/10/original_1.png?1386744264"},{"id":18,"name":"Textile","icon":"/icons/images/46/original_37.png?1386745050"}]}

6 个答案:

答案 0 :(得分:1)

试试这个:

     NSURL * url = [NSURL URLWithString:@"http://myurl.json"];
     NSData * data = [NSData dataWithContentsOfURL:url];

    NSDictionary * json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    NSArray *catsArray = [NSArray arrayWithArray:[json objectForKey:@"categories"]];

    cats = [[NSMutableArray alloc] init];

    for (int i = 0; i < catsArray.count; i++) {
        NSString * CatId = [[catsArray objectAtIndex:i] objectForKey:@"id"];
        NSString * CatName = [[catsArray objectAtIndex:i] objectForKey:@"name"];
        NSString * CatIcon = [[catsArray objectAtIndex:i] objectForKey:@"icon"];
        categories * cat = [[categories alloc] initWithCId:CatId andCName:CatName andCIcon:CatIcon];

        [cats addObject:cat];
    }

答案 1 :(得分:0)

试试这个:

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
json = [dict objectForKey:@"categories"];

答案 2 :(得分:0)

当你尝试这个时:

NSString * CatId = [[json objectAtIndex:i] objectForKey:@"id"];

它转换为:Array - &gt;字典 - &gt;串

但是你可以看到你不想要这个 你想要的:字典 - &gt;数组 - &gt;字典 - &gt;字符串

在代码中:

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSArray *array = [dict objectForKey:@"categories"];
NSString * CatId = [[array objectAtIndex:0] objectForKey:@"id"];

答案 3 :(得分:0)

试试这个。 。 。

NSURL * url = [NSURL URLWithString:@"http://myurl.json"];
NSData * data = [NSData dataWithContentsOfURL:url];

NSDictionary * json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

NSArray * cat = [json objectForKey:@"categories"];

for(NSDictionary * tempdict in cat)
{
    NSString * CatId = [NSString stringWithFormat:@"%@",[tempdict objectForKey:@"id"]];
    NSString * CatName = [tempdict objectForKey:@"name"];
    NSString * CatIcon = [tempdict objectForKey:@"icon"];
    categories * cat = [[categories alloc] initWithCId:CatId andCName:CatName andCIcon:CatIcon];

}

答案 4 :(得分:0)

这应该有效:将Json视为NSMutableArray,然后轻松地逐一获取对象的数组:

 NSMutableArray  *userDetails = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil];
    NSLog(@"User Det %@", userDetails);
    if (userDetails == nil || [userDetails count] == 0) {

    } else {

          // 3. iterate the array; each element is a dictionary...
          for (NSDictionary *lesson in userDetails)
          {

             NSString * CatId = [[catsArray objectAtIndex:i] objectForKey:@"id"];
             NSString * CatName = [[catsArray objectAtIndex:i] objectForKey:@"name"];
             NSString * CatIcon = [[catsArray objectAtIndex:i] objectForKey:@"icon"];
             categories * cat = [[categories alloc] initWithCId:CatId andCName:CatName andCIcon:CatIcon];
             [cats addObject:cat];
           }

}

答案 5 :(得分:0)

  NSURL * url = [NSURL URLWithString:@"http://myurl.json"];
    NSData * data = [NSData dataWithContentsOfURL:url];

    NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

    NSArray *myArray = [jsonData objectForKey:@"categories"];

    cats = [[NSMutableArray alloc] init];

    for (NSDictionary *temp in myArray) {

        NSString * CatId = [NSString stringWithFormat:@"%d",[temp objectForKey:@"id"]];
        NSString * CatName = [temp objectForKey:@"name"];
        NSString * CatIcon = [temp objectForKey:@"icon"];
        categories * cat = [[categories alloc] initWithCId:CatId andCName:CatName andCIcon:CatIcon];

        [cats addObject:cat];
   }