以下の例のように、JSON オブジェクトのパラメーターを作成したいと思います。私は iOS 5 デバイスで作業したことがあり、NSJSONSerialization API を使用してこれを実現できました。たとえば、汎用関数「makeJSONObject()」を作成し、それを使用しています。
Sample Payload 1:
{
token: "kjsdfjl23kkj23kk"
entries: [
{
"title": "welcome",
"name": "myself",
"date": "2012-02-06T00:14:20Z",
},{
"title": "Hi",
"name": "martin",
"date": "2012-02-06T00:14:20Z",
}
]
}
Sample Payload 2:
{
"email" : "me@company.com",
"password" : "pswrd"
}
コード:
NSString *jsonRequest = [appDelegate makeJSONObject:[NSArray arrayWithObjects: emailStr, passwordStr, nil] :[NSArray arrayWithObjects: @"email", @"password", nil] ];
-(NSString *) makeJSONObject :(NSArray *)objects :(NSArray *)keys
{
NSString *theBodyString = NULL;
NSDictionary *data = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
//NSLog(@"data: %@", data);
NSError *writeError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:NSJSONWritingPrettyPrinted error:&writeError];
theBodyString = [[NSString alloc] initWithData:jsonData encoding:NSASCIIStringEncoding];
return theBodyString;
}
しかし、今すぐ 4.0 デバイスをサポートしたいのですが、現在 NSJSONSerialization API を使用できません。私はSBJsonまたはそのようなものを使用する必要があるかもしれませんが、私にはわかりません。上記のジェネリック関数を変更して、SBJsonまたはサードパーティのパーサークラスを使用するにはどうすればよいですか?
助けてください!ありがとうございました。