Search code examples
iphonejsonparsingnsmutabledictionarysbjson

Complex Json Data and NSMutableDictionary parsing issue


Hi I'm using SBJson for moving Json data in and out of NSMutableDictionar, Im Building the main NSMutableDictionary from few others NSMutableDictionary like this

- (NSMutableDictionary *) getGeneral{
    NSMutableDictionary *pType = [[NSMutableDictionary alloc]init];
    [pType setObject:[NSNumber numberWithInteger:3] forKey:@"Ptype"];

    NSMutableDictionary *session = [[NSMutableDictionary alloc]init];
    [session setObject:[NSNumber numberWithInteger:-1] forKey:@"user_id"];
    [session setObject:@"3" forKey:@"device_token"];
    [session setObject:[NSNumber numberWithInteger:-1] forKey:@"customer_id"];
    [session setObject:@"3" forKey:@"client_time"];

    NSMutableDictionary *Error = [[NSMutableDictionary alloc]init];
    [Error setObject:[NSNumber numberWithInteger:-1] forKey:@"error_code"];
    [Error setObject:@"3" forKey:@"error_message"];

    NSMutableDictionary *Successful = [[NSMutableDictionary alloc]init];
    [Successful setObject:[NSNumber numberWithInteger:-1] forKey:@"success_code"];
    [Successful setObject:@"3" forKey:@"success_message"];

    NSMutableDictionary *Details = [[NSMutableDictionary alloc]init];
    [Details setObject:@"3" forKey:@"user_name" ];
    [Details setObject:@"3" forKey:@"user_password" ];
    [Details setObject:[NSNumber numberWithInteger:-1] forKey:@"StartCallID"];
    [Details setObject:@"3" forKey:@"StartDate" ];
    [Details setObject:@"3" forKey:@"EndDate"];

    NSMutableDictionary *General = [[NSMutableDictionary alloc]init];  
    [General setObject:pType forKey:@"Ptype"];
    [General setObject:session forKey:@"Session"];
    [General setObject:Error forKey:@"Error"];
    [General setObject:Successful forKey:@"Successful"];
    [General setObject:Details forKey:@"Details"];
    return General;
}

and then i assign data into it i expected to get this Json structure:

{
"Ptype":[{"Ptype":-1}],
"Session":[{
"user_id":-1, 
   "device_token":" ", 
"customer_id":-1,  
 "client_time":"",    
}],
"Error":[{"error_code":-1,
"error_message":""}],
"Successful":[{"success_code":-1,
"success_message":""}],
"Details":[{
"user_name":" ",    
"user_password":" ",  
"StartCallID":-1,   
 "StartDate":" ",  
  "EndDate":" "      
}]}

but there is no "]" or "[" in my json it looks like this, the order also change but this is not a problem, i take care of it on the server, issue is no square brackets

    {"Session":
{"customer_id":-1,
"client_time":"3",
"user_id":-1,
"device_token":"3"},
"Error":{"error_code":-1,"error_message":"3"},
"Successful":{"success_code":-1,"success_message":"3"},
"Details":{"StartCallID":-1,
"user_password":"gg",
"user_name":"ff",
"StartDate":"3",
"EndDate":"3"},
"Ptype":{"Ptype":3}}

any one know on this issue, i need multiple items with the same name and this is the json standart for it

Thanks


Solution

  • Square brackets surrounds an array and you only have dictionaries. The key is unique within each dictionary.

    For example to put customer_id in a dictionary within an array:

    
        NSArray *myArray = [NSArray arrayWithObjects:
                            [NSDictionary dictionaryWithObjectsAndKeys:
                             @"-1",
                             @"customer_id",
                             nil] nil];
    

    Link of interest Understand JSON 3min lesson