如何从Objective-C中的Json响应获取值

从Json响应中获取数据时遇到问题。

这是一个示例数据结构:

( { AT = "<null>"; DId = 0; DO = 0; PLId = 33997; PdCatList = ( { PLId = 33997; PPCId = 0; pdList = ( { IsDis = 0; IsPS = 0; IsT = 1; PA = 1; PCId = 119777; } ); } ); PdId = 0; SId = 0; Sec = 1; }, { AT = "<null>"; DId = 0; DO = 11; Dis = 0; PLId = 34006; PdCatList = ( { PLId = 34006; PPCId = 0; pdList = ( { IsDis = 0; IsPS = 0; IsT = 1; PA = 1; PCId = 119830; }, { IsDis = 0; IsPS = 0; IsT = 1; PA = 1; PCId = 119777; } ); }, { PLId = 33997; PPCId = 0; pdList = ( { IsDis = 0; IsPS = 0; IsT = 1; PA = 1; PCId = 119777; } ); } ); PdId = 0; SId = 0; Sec = 1; }, ) 

我将如何parsing结果? 我想直接得到一个值列表。 如果我在tupel中有几个值,例如执行者PdCatList,pdList。 我将如何访问这些值? 谁能帮我

谢谢

我的代码是

 NSError *error; Array1 = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; for(int i=0;i<[Array1 count];i++) { NSDictionary *dict1 = [Array1 objectAtIndex:i]; NSLog(@"Array1.....%@",dict1); Array2=[dict1 valueForKey:@"PdCatList"]; for(int i=0;i<[Array2 count];i++) { NSDictionary *dict2 = [Array2 objectAtIndex:i]; NSLog(@"Array2.....%@",dict2); Array3=[dict2 valueForKey:@"pdList"]; for(int i=0;i<[Array3 count];i++) { NSDictionary *dict3 = [Array3 objectAtIndex:i]; NSLog(@"Array3.....%@",dict3); } } } 

尝试这个…

 NSError *error; Array1 = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; for(int i=0;i<[Array1 count];i++) { NSDictionary *dict1 = [Array1 objectAtIndex:i]; ATArray =[dict1 valueForKey:@"AT"]; DIdArray =[dict1 valueForKey:@"DId"]; DOArray =[dict1 valueForKey:@"DO"]; PLIdArray =[dict1 valueForKey:@"PLId"]; etc... Array2=[dict1 valueForKey:@"PdCatList"]; for(int i=0;i<[Array2 count];i++) { NSDictionary *dict2 = [Array2 objectAtIndex:i]; PLIdArray =[dict2 valueForKey:@"PLId"]; PPCIdArray =[dict2 valueForKey:@"PPCId"]; etc… Array3=[dict2 valueForKey:@"pdList"]; for(int i=0;i<[Array3 count];i++) { NSDictionary *dict3 = [Array3 objectAtIndex:i]; IsDisArray =[dict3 valueForKey:@"IsDis"]; IsPSArray =[dict3 valueForKey:@"IsPS"]; IsTArray =[dict3 valueForKey:@"IsT"]; PAArray =[dict3 valueForKey:@"PA"]; PCIdArray =[dict3 valueForKey:@"PCId"]; } } } 

我认为你在这里需要的是了解什么是JSON响应而不是答案从你的JSON响应获取一些对象的值。

如果你想要一些关于JSONparsing的细节解释,那么你可以看看NSJSONSerialization类的参考 。 一切都在那里,或者你可以看看我的答案 。


理解概念。 这取决于你在你的JSON里面有什么。 如果它是一个Array( [ ]内的值),那么你必须保存在NSArray ,如果它是一个字典( { }内的值),然后另存为NSDictionary ,如果你有单个值,如string,整数,双,那么你必须保存使用适当的Objective-C数据types。

对于一些简单的例子的细节,你可以检查我的答案,从这个问题。

使用JSONKit( https://github.com/johnezang/JSONKit ):

 NSString *yourJSONString = ... NSArray *responseArray = [yourJSONString objectFromJSONString]; for(NSDictionary *responseDictionary in responseArray) { NSString *atString = [responseDictionary objectForKey:@"AT"]; ... NSArray *pdCatListArray = [responseDictionary objectForKey:@"PdCatList"]; ...here you can get all values you want,if you want to get more details in PdCatList,use for in pdCatListArray ,you can do what you want. } 
 Use following method: NSDictionary *mainDict; SBJSON *jsonParser = [[SBJSON alloc]init]; if([[jsonParser objectWithString:responseString] isKindOfClass:[NSDictionary class]]) { mainDict=[[NSDictionary alloc]initWithDictionary:[jsonParser objectWithString:responseString]]; } NSDictionary *firstDict=[NSDictionary alloc]initWithDictionary:[mainDict valueForKey:@""]; 

你必须parsingstring的JSON框架添加到NSDictionary中。 从这里使用zip文件

  1. 打开文件夹并将类文件夹重命名为“JSON”。
  2. 复制JSON文件夹并包含在您的项目中。
  3. 在控制器中导入类似下面的文件,你想parsingJSONstring。

     #import "SBJSON.h" #import "NSString+SBJSON.h" 
  4. 现在,像下面一样parsing你的响应string到NSDictionary

     NSMutableDictionary *dictResponse = [strResponse JSONValue]; 

您可以使用KVC来访问JSON中的嵌套属性。 您需要了解KVC和点语法以及集合运算符

将JSON映射到对象(例如RestKit)的框架严重依赖于KVC。

在您的示例之后,您可以获得所有PdCatList对象的列表:

 //sample data NSArray *json = @[ @{@"PLId" : @33997, @"PdCatList" : @{@"PLId": @33998, @"PPCId" : @1, @"pdList" : @{ @"PCId" : @119777 }} }, @{@"PLId" : @33999, @"PdCatList" : @{@"PLId": @4444, @"PPCId" : @0, @"pdList" : @{ @"PCId" : @7777 }}} ]; //KVC NSArray *pdCatLists = [json valueForKeyPath:@"@unionOfObjects.PdCatList"]; 

有了这个,你可以,例如,做一个非常基本的对象映射(这不关心关系)

在PdCatList.h中

 @interface PdCatList : NSObject @property (readonly, strong, nonatomic) NSNumber *PLId; @property (readonly, strong, nonatomic) NSNumber *PPCId; + (instancetype)listWithDictionary:(NSDictionary *)aDictionary; @end 

在PdCatList.m中

 @implementation PdCatList - (void)setValue:(id)value forUndefinedKey:(NSString *)key { @try { [super setValue:value forUndefinedKey:key]; } @catch (NSException *exception) { NSLog(@"error setting undefined key: %@, exception: %@", key, exception); }; } + (id)listWithDictionary:(NSDictionary *)aDictionary { PdCatList *result = [[self alloc] init]; [result setValuesForKeysWithDictionary:aDictionary]; return result; } @end 

得到json对象之后

 NSArray *pdCatLists = [json valueForKeyPath:@"@unionOfObjects.PdCatList"]; [pdCatLists enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { PdCatList *each = [PdCatList listWithDictionary:obj]; }]; 

但是,如果你想要的只是平坦的JSON,你必须使用recursion,并创build一个类似于下面的类别。

在NSJSONSerialization + FlattenedJSON.h

 @interface NSJSONSerialization (FlattenedJSON) + (void)FlattenedJSONObjectWithData:(NSData *)data completionSuccessBlock:(void(^)(id aJson))onSuccess failure:(void(^)(NSError *anError))onFailure; @end 

在NSJSONSerialization + FlattenedJSON.m

 #import "NSJSONSerialization+FlattenedJSON.h" @implementation NSJSONSerialization (FlattenedJSON) + (void)FlattenedJSONObjectWithData:(NSData *)data completionSuccessBlock:(void (^)(id))onSuccess failure:(void (^)(NSError *))onFailure { NSError *error; id object = [self JSONObjectWithData:data options:kNilOptions error:&error]; if (error) { onFailure(error); } else { NSMutableArray *result = [NSMutableArray array]; [self flatten:object inArray:result]; onSuccess([result copy]); } } + (void)flatten:(id)anObject inArray:(NSMutableArray *)anArray { if ([anObject isKindOfClass:NSDictionary.class]) { [((NSDictionary *)anObject) enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { [self flatten:obj inArray:anArray]; }]; } else if ([anObject isKindOfClass:NSArray.class]) { [((NSArray *)anObject) enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { [self flatten:obj inArray:anArray]; }]; } else { [anArray addObject:anObject]; } } @end