【问题标题】:Complex Json Data and NSMutableDictionary parsing issue复杂的 Json 数据和 NSMutableDictionary 解析问题
【发布时间】:2012-02-14 17:24:00
【问题描述】:

嗨,我正在使用 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 标准

谢谢

【问题讨论】:

    标签: iphone json parsing nsmutabledictionary sbjson


    【解决方案1】:

    方括号包围着一个数组,而你只有字典。 每个字典中的键都是唯一的。

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

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

    感兴趣的链接Understand JSON 3min lesson

    【讨论】:

    • 感谢 Fredrik,您解决了我的问题 Shimon Wiener
    猜你喜欢
    • 2015-06-02
    • 2020-08-18
    • 2016-06-18
    • 2020-10-21
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多