0

I'm aware of how to parse a JSON data with all the key pair values. Check the below JSON

{ "a": [ "a1", "a2", "a3" ], "b": [ "b1", "b2", "b3" ] }

in this the key values a and b are not static key values . They are dynamic key values. How do i parse this one?

4 Answers 4

1

If you know they are Arrays:

NSDictionary *parsedData = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

for (NSString *key in [parsedData allKeys])
{
  // you have now a key to an array
}
Sign up to request clarification or add additional context in comments.

Comments

0
   //Get json data in Dictionary
json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error];

NSLog(@"%@",json);

NSArray * responseArr = json[@"Deviceinfo"];   // Here you need pass your key

for(NSDictionary * dict in responseArr)
{

    [delegate.firstArray addObject:[dict valueForKey:@"a"]];
}

Try this code...

Comments

0

To convert JSON to NSDictionary:

NSError *error= nil; 
NSDictionary *parsedDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

parsedDictionary will be

parsedDictionary = {
a = [a1,a2,a3],
b = [b1,b2,b3]
};

To get all keys in dictionary

NSArray *allKeys = [parsedDictionary allKeys];

To get a and b arrays in dictionary

for (NSString *key in allKeys)
{

NSArray *a = parsedDictionary[key];

}

Comments

0

You can use my method for json parsing,

Parse Method:

    -(void)jsonDeserialize:(NSString *)key fromDict:(id)content completionHandler:(void (^) (id parsedData, NSDictionary *fromDict))completionHandler{
    if (key==nil && content ==nil) {
        completionHandler(nil,nil);
    }
    if ([content isKindOfClass:[NSArray class]]) {
        for (NSDictionary *obj in content) {
          [self jsonDeserialize:key fromDict:obj completionHandler:completionHandler];
        }
    }
    if ([content isKindOfClass:[NSDictionary class]]) {
        id result = [content objectForKey:key];
        if ([result isKindOfClass:[NSNull class]] || result == nil) {
            NSDictionary *temp = (NSDictionary *)content;
            NSArray *keys = [temp allKeys];
            for (NSString *ikey in keys) {
             [self jsonDeserialize:key fromDict:[content objectForKey:ikey] completionHandler:completionHandler];
            }
        }else{
            completionHandler(result,content);
        }
    }
}

Method Call:

 NSData *content = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Sample" ofType:@"json"]];
    NSError *error;

//to get serialized json data...

   id dictionary = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:&error];

//get data for key called GetInfo

     [self jsonDeserialize:@"GetInfo" fromDict:dictionary completionHandler:^(id parsedData, NSDictionary *fromDict) {
            NSLog(@"%@ - %@",parsedData,fromDict);
        }];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.