How to iterate JSON object in ios

后端 未结 1 651
走了就别回头了
走了就别回头了 2021-01-15 09:43

I create json object like this

id json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions

相关标签:
1条回答
  • 2021-01-15 10:05

    Here is one way to do it. I'll be happy to get into more details if you make your question more specific.

        NSString *jsonString = @"[{\"id\": \"1\", \"name\":\"Aaa\"}, {\"id\": \"2\", \"name\":\"Bbb\"}]";
        NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
        NSError *e = nil;
        NSArray *jsArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&e];
    
    if (!jsArray) {
      NSLog(@"Error parsing JSON: %@", e);
    } else {
       for(NSDictionary *item in jsArray) {
          NSLog(@"Item: %@", item);
          NSLog(@"%@",[item objectForKey:@"id"]);
          NSLog(@"%@",[item objectForKey:@"name"]);
    
       }
    }
    
    0 讨论(0)
提交回复
热议问题