复杂的Json Data和NSMutableDictionary解析问题

时间:2012-02-14 17:24:01

标签: iphone json parsing nsmutabledictionary sbjson

您好我正在使用SBJson将Json数据移入和移出NSMutableDictionar,我在其他几个NSMutableDictionary中构建主要的NSMutableDictionary

- (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;
}

然后我将数据分配给它我希望得到这个Json结构:

{
"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":" "      
}]}

但是我的json中没有“]”或“[”它看起来像这样,顺序也改变但这不是问题,我在服务器上处理它,问题是没有方括号

    {"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}}

任何人都知道这个问题,我需要多个具有相同名称的项目,这是json标准的

由于

1 个答案:

答案 0 :(得分:1)

方括号围绕一个数组,您只有字典。 密钥在每个字典中都是唯一的。

例如,将customer_id放入数组中的字典中:


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

感兴趣的链接Understand JSON 3min lesson