I have a problem with fetching data from Json response.
Here is an example data structure :
(
{
AT = \"\";
DId = 0;
Try this...
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"];
}
}
}
I think what you require here is to understand what a JSON response is rather than the Answer to get the values of some objects from your JSON response.
If you want some detail explanation about JSON Parsing then you can take a look at NSJSONSerialization Class Reference. Everything is given there or you can take a look at my Answer.
Understand the Concept. It depends on what you have inside your JSON
. If it's an Array ( Values inside [ ]
) then you have to save in NSArray
, if it's a Dictionary ( Values inside { }
) then save as NSDictionary
and if you have single values like string , integer, double then you have to save them using appropriate Objective-C Data types.
For some simple details with example , you can check my Answer from this question.
Use 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.
}
You have to add JSON framework which is parse string into NSDictionary. Use zip file from here
Import header file like below in controller where you want to parse JSON String.
#import "SBJSON.h"
#import "NSString+SBJSON.h"
Now, Parse your response string in to NSDictionary like below.
NSMutableDictionary *dictResponse = [strResponse JSONValue];
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:@""];
You can use KVC to access the nested properties in the JSON. You need to know about KVC and dot syntax and Collection operators
Frameworks that map JSON to objects, such as RestKit rely heavily on KVC.
Following your sample, you could get a list of all PdCatList objects:
//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"];
With this you can, for example, make a very basic object mapping (which does not take care of relationships)
In PdCatList.h
@interface PdCatList : NSObject
@property (readonly, strong, nonatomic) NSNumber *PLId;
@property (readonly, strong, nonatomic) NSNumber *PPCId;
+ (instancetype)listWithDictionary:(NSDictionary *)aDictionary;
@end
In 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
After getting the json object
NSArray *pdCatLists = [json valueForKeyPath:@"@unionOfObjects.PdCatList"];
[pdCatLists enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
PdCatList *each = [PdCatList listWithDictionary:obj];
}];
However, If what you want is just to flatten the json, you must use recursion and create a category similar to the following.
In NSJSONSerialization+FlattenedJSON.h
@interface NSJSONSerialization (FlattenedJSON)
+ (void)FlattenedJSONObjectWithData:(NSData *)data completionSuccessBlock:(void(^)(id aJson))onSuccess failure:(void(^)(NSError *anError))onFailure;
@end
In 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