gpt4 book ai didi

objective-c - 在 objective-c 中创建 json 对象数组

转载 作者:太空狗 更新时间:2023-10-30 03:35:11 25 4
gpt4 key购买 nike

我是 objective-c 的新手,需要提交 json 对象的集合。

我写了以下内容:

NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
id, @"id",
toClientGroupType, @"toClientGroupType",
dueDate, @"dueDate",
actionDate, @"actionDate",
campaignType, @"campaignType",
campaignCategory, @"campaignCategory",
businessId, @"businessId",
promotion, @"promotion",
product, @"product",
contentF, @"content",
subject, @"subject",
nil];
NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
NSLog(@"jsonData as string:\n%@", jsonString);
[request setURL:[NSURL URLWithString:@"https://services-dev.a.com/api/channels"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:jsonData2];

我有两个问题:

一个。 jsonData 作为 String 的输出是

{
"toClientGroupType" : "VIP",
"id" : "1",
"dueDate" : "2012-09-03 10:25:42 +0000",
"actionDate" : "2012-09-03 10:25:42 +0000",
"campaignType" : "ONE_TIME",
"businessId" : "150",
"campaignCategory" : "SALE"
}

如您所见 - 我缺少 3 个声明的字段:contentproductsubject

B.我实际上需要提交一个对象数组,所以请求将是这样的:

[{
"toClientGroupType" : "VIP",
"id" : "1",
"dueDate" : "2012-09-03 10:25:42 +0000",
"actionDate" : "2012-09-03 10:25:42 +0000",
"campaignType" : "ONE_TIME",
"businessId" : "150",
"campaignCategory" : "SALE"
}]

我该怎么做,有什么问题吗?

最佳答案

NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
id, @"id",
toClientGroupType, @"toClientGroupType",
dueDate, @"dueDate",
actionDate, @"actionDate",
campaignType, @"campaignType",
campaignCategory, @"campaignCategory",
businessId, @"businessId",
promotion, @"promotion",
product, @"product",
contentF, @"content",
subject, @"subject",
nil];


NSMutableArray * arr = [[NSMutableArray alloc] init];

[arr addObject:jsonDictionary];
NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
NSLog(@"jsonData as string:\n%@", jsonString);

检查这个

关于objective-c - 在 objective-c 中创建 json 对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12246135/

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