在 Objective-C 中解析 JSON 响应字符串

Pra*_*ain 2 json objective-c ios

"[
  {
    \"TheatreName\": \"FunCinemas\",
    \"TheatreId\": 1,
    \"City\": \"Chandigarh\"
  },
  {
    \"TheatreName\": \"PVRElanteMall\",
    \"TheatreId\": 2,
    \"City\": \"Chandigarh\"
  },
  {
    \"TheatreName\": \"PiccadilySquare\",
    \"TheatreId\": 3,
    \"City\": \"Chandigarh\"
  }
]"
Run Code Online (Sandbox Code Playgroud)

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

Jan*_*aya 6

我试图用你的 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];
    }

}
Run Code Online (Sandbox Code Playgroud)