我是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个我宣布的fiels:content
,product
和subject
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"
}]
我该怎么办?出了什么问题?
答案 0 :(得分:12)
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);
结帐这个
答案 1 :(得分:3)
NSError *error;
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
@"1", @"id",
@"test", @"toClientGroupType",
@"test", @"dueDate",
@"test", @"actionDate",
@"test", @"campaignType",
@"test", @"campaignCategory",
@"test", @"businessId",
@"test", @"promotion",
@"test", @"product",
@"test", @"content",
@"test", @"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);
输出: - [ { “主题”:“测试”, “toClientGroupType”:“test”, “id”:“1”, “dueDate”:“测试”, “actionDate”:“测试”, “campaignType”:“测试”, “businessId”:“测试”, “产品”:“测试”, “内容”:“测试”, “campaignCategory”:“测试”, “促销”:“测试” } ]
查看促销,产品,内容和主题中的数据。不应为零或空
答案 2 :(得分:1)
答案 3 :(得分:1)
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSError *error;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingAllowFragments error:&error];
NSArray *categoryArray= [dic valueForKey:@"SELECTED OBJECT KEY"];
NSLog(@"category%@",categoryArray);
}
类别:显示类别数组内容