在Objective - C

时间:2016-09-20 11:19:14

标签: ios objective-c json

"[
  {
    \"TheatreName\": \"FunCinemas\",
    \"TheatreId\": 1,
    \"City\": \"Chandigarh\"
  },
  {
    \"TheatreName\": \"PVRElanteMall\",
    \"TheatreId\": 2,
    \"City\": \"Chandigarh\"
  },
  {
    \"TheatreName\": \"PiccadilySquare\",
    \"TheatreId\": 3,
    \"City\": \"Chandigarh\"
  }
]"

我想解析这些数据,即将所有对象Theatrename,id,city

分开

1 个答案:

答案 0 :(得分:3)

我尝试使用您的JSON值

创建演示场景

以下是代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *str = @"[{\"TheatreName\": \"FunCinemas\",\"TheatreId\": 1,\"City\":\"Chandigarh\"},{\"TheatreName\":\"PVRElanteMall\",\"TheatreId\": 2,\"City\": \"Chandigarh\"},{\"TheatreName\": \"PiccadilySquare\",\"TheatreId\": 3,\"City\": \"Chandigarh\"}]";
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];

    NSError *err = nil;
    NSArray *jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&err];

    NSMutableArray *arrResult = [NSMutableArray array];

    for (NSDictionary *dict in jsonData) {

        NSLog(@"TheatreName %@", dict[@"TheatreName"]);
        NSLog(@"TheatreId %@", dict[@"TheatreId"]);
        NSLog(@"City %@", dict[@"City"]);

        [arrResult addObject:dict];
    }

}