iOS在POST中发送jSOn数组

时间:2014-08-01 08:49:55

标签: ios json nsarray

您好我想创建一个以下结构的jSon数组:

"Create Account":{ 
"RegistryNumber":"",
"People":[{
"PeopleId":"",
"email":"",
"pass":"",
}]
}

我正在使用以下代码来执行此操作:

NSDictionary *content0 = [NSDictionary dictionaryWithObjectsAndKeys:
                              @"PeopleId", @"0",
                              @"email", @"email",
                              @"pass", @"pass",nil];

  NSArray *peopledetails = [NSArray arrayWithObjects:content0,nil];
NSMutableDictionary *peopleDict = [[NSMutableDictionary alloc] init]; 
[peopleDict setObject:@"content0" forKey:@"People"];

NSMutableDictionary *details = [[NSMutableDictionary alloc] init];  
[details setObject:@"RegistryNumber" forKey:@"RegistryNumber"];
[details setObject:@"peopleDict" forKey:@"People"];
NSMutableDictionary *MainDict = [[NSMutableDictionary alloc] init]; 
[MainDict setObject:@"details" forKey:@"Create Account"];

但是这给了我一个来自服务器的错误。我有其他api,当数组不在图片中时工作正常。你能帮我解决这个问题吗!!!

3 个答案:

答案 0 :(得分:1)

更改

[peopleDict setObject:@"content0" forKey:@"People"];

[peopleDict setObject:peopledetails forKey:@"People"];

删除对象的所有@“”。

@"details"是一个字符串,details是一个对象

或者这是简单的方法

  NSDictionary *dict=@{
                         @"Create Account": @{
                                 @"RegistryNumber": @"",
                                 @"People": @[
                                         @{
                                             @"PeopleId": @"",
                                             @"Email": @"",
                                             @"pass":@""
                                             }
                                         ]
                                 }
                         };

答案 1 :(得分:1)

- (void)simpleJsonParsing
{
    //  URL request with server
    NSHTTPURLResponse *response = nil;
    NSString *jsonUrlString = [NSString stringWithFormat:@"URL"];
    NSURL *url = [NSURL URLWithString:[jsonUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    //-- Get request and response though URL
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

    //-- JSON Parsing
    NSMutableArray *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"Result = %@",result);

    for (NSMutableDictionary *dic in result)
    {
         NSString *string = dic[@"Create Account"];
        if (string)
        {
             NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
             dic[@"Create Account"] = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        }
        else
        {
             NSLog(@"Error in response");
        }
    }

}

答案 2 :(得分:0)

我认为你的结构错误

NSDictionary *content0 = [NSDictionary dictionaryWithObjectsAndKeys:
                              @"PeopleId", @"0",
                              @"email", @"email",
                              @"pass", @"pass",nil];


postDict = @{@"Create Account":@{@"RegistryNumber":"","People":content0}}

现在将其转换为jason并发送到您的服务器