xcode 在目标c中创建json对象数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12246135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:24:31  来源:igfitidea点击:

Create array of json objects in objective c

objective-cxcode

提问by Dejell

I am new to objective-c and need to submit collection of json objects.

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

I wrote the following:

我写了以下内容:

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

I have 2 problems:

我有两个问题:

A. The output of jsonData as String is

A. jsonData as 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"
    }

As you see - I am missing 3 fiels which I declared: content, productand subject

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

B. I actually need to submit an array of objects so the request will be like this:

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

How can I do it and what is wrong?

我该怎么做,出了什么问题?

回答by prashant

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

Checkout this one

结帐这个

回答by prashant

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

Output :- [ { "subject" : "test", "toClientGroupType" : "test", "id" : "1", "dueDate" : "test", "actionDate" : "test", "campaignType" : "test", "businessId" : "test", "product" : "test", "content" : "test", "campaignCategory" : "test", "promotion" : "test" } ]

输出:- [ { "subject" : "test", "toClientGroupType" : "test", "id" : "1", "dueDate" : "test", "actionDate" : "test", "campaignType" : "测试”,“businessId”:“测试”,“产品”:“测试”,“内容”:“测试”,“campaignCategory”:“测试”,“促销”:“测试”}]

check out the data in to promotion, product, content and subject.it should not be nil or null

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

回答by mano

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

category: Display Category array content

category:显示类别数组内容

回答by E. Lüders

To problem A:

问题A:

I think your fields are missing because they contain nil values. Keys that contain nil values are not considered when you use NSJSONSerialization

我认为您的字段缺失,因为它们包含 nil 值。使用 NSJSONSerialization 时不考虑包含 nil 值的键

To problem B:

问题B:

prashant has posted a good solution

prashant 发布了一个很好的解决方案