iOS SDK原生JSON解析

时间:2023-03-08 16:11:10
- (IBAction)touchReadButton:(id)sender
{
NSData *jsonData = [[NSData alloc] initWithContentsOfFile:JSON_PATH];
NSError *error = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error;];
  if (jsonObject != nil && error == nil)
  {
  NSLog(@"Successfully deserialized...");
  if ([jsonObject isKindOfClass:[NSDictionary class]])
{
NSDictionary *deserializedDictionary = (NSDictionary *) jsonObject;
NSLog(@"Dersialized JSON Dictionary = %@", deserializedDictionary);
}
else if([jsonObject isKindOfClass:[NSArray class]])
{
NSArray *deserializedArray = (NSArray *)jsonObject;
NSLog(@"Dersialized JSON Array = %@", deserializedArray);
}
       else
{
           NSLog(@"An error happened while deserializing the JSON data.");
}
}
}