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

私には2つの問題があります:

A. String としての 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"
    }

ご覧のとおり、宣言した 3 つのフィールドがありません: contentproductおよび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"
}]

どうすればそれを行うことができますか?何が問題なのですか?

4

4 に答える 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);

これをチェックアウト

于 2012-09-03T10:52:51.863 に答える
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);

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

プロモーション、製品、コンテンツ、件名のデータをチェックアウトします。nil または null であってはなりません

于 2012-09-03T11:26:50.457 に答える
1

問題Aへ:

フィールドにnil値が含まれているため、フィールドが欠落していると思います。NSJSONSerializationを使用する場合、nil値を含むキーは考慮されません。

問題Bへ:

prashantは良い解決策を投稿しました

于 2012-09-03T10:57:54.007 に答える