gpt4 book ai didi

iOS 在 POST 中发送 JSON 数组

转载 作者:行者123 更新时间:2023-11-29 10:39:54 26 4
gpt4 key购买 nike

我想创建一个具有以下结构的 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 可以正常工作。

最佳答案

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

}

关于iOS 在 POST 中发送 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25076309/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com