在Objective-C中创建JSON对象的数组

7

我是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];

我有两个问题:

A. 将jsonData输出为字符串时的结果是:

{
      "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"
    }

您看到了吗 - 我缺少了三个我声明的字段: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"
}]

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

4个回答

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);

来看这个


我得到了“NSMutableArray”没有可见的@interface声明选择器“addobject:”。 - Dejell
谢谢,它正在工作,但我的第一个问题仍然没有解决。在JSON字符串中未解析促销、产品、内容和主题。 - Dejell
请打印促销、产品、内容和主题中的数据。 - prashant
抱歉,我的错误是将促销初始化为int类型,因此无法将其添加到字典中。不幸的是,我没有收到任何警告或错误提示。 - Dejell
大多数情况下尽量使用NSNumber。 - prashant

4
 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);

输出结果: [ { "主题" : "测试", "客户端组类型" : "测试", "ID" : "1", "到期日期" : "测试", "行动日期" : "测试", "活动类型" : "测试", "业务编号" : "测试", "产品" : "测试", "内容" : "测试", "活动类别" : "测试", "促销" : "测试" } ]

请检查促销、产品、内容和主题中的数据,它们不应为空或为零。


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

类别:显示类别数组内容


获取您的URL,例如(http://example.com) NSString *path = @""; NSURL *urlpath = [NSURL URLWithString:path]; NSURLRequest *request = [NSURLRequest requestWithURL:urlpath cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:100.0 ]; NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self ]; [connection start]; 从上述字典中获取数据。 - mano

1
针对问题A:
我认为你的字段缺失是因为它们包含了nil值。当使用NSJSONSerialization时,包含nil值的键不会被考虑。
针对问题B: prashant发布了一个很好的solution

我在 arr 行得到了“NSMutableArray”没有可见的 @interface 声明选择器“addobject:”。 - Dejell
关于问题A,这些字段不是空的。我明确地设置了它们:例如:NSString *product = @"EMAIL"; - Dejell
嗯,这很奇怪。你考虑过使用像 JSONKit 这样的外部 JSON 库吗?我在我的许多项目中都使用它,从未遇到过任何问题。 - E. Lüders
好的,那么我没有其他想法,只能再次检查您的变量,因为您的代码似乎是正确的。顺便说一下,在您的线程中,您没有提到输出中也缺少促销。 - E. Lüders

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接