我有很多 json 对象,例如:
{"id":"1","list":"456","name":"a"}, {"id":"2","list":"456","name":"b"}, {"id":"3","list":"456","name":"c"}
它们存储在 NSDictionary
dic1, dic2, dic3 中,然后我使用 NSArray
存储这些 dic,
这是我的问题,我如何发布这些词典?
我知道发布一个 json 对象,这是我的代码
NSString *url = [SERVERPREFIX stringByAppendingString:urlString];
DLog(@"url:%@", url);
NSURL *reqURL = [NSURL URLWithString:url];
NSData ***__jsonData**;
// Be sure to properly escape your url string.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:reqURL];
[request setHTTPMethod:@"POST"];
[request setHTTPBody: **__jsonData**];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", (unsigned int)[__jsonData length]] forHTTPHeaderField:@"Content-Length"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
那么,问题是 __jsonData 应该是什么?
最佳答案
jsonData
应该是:
jsonData = [NSJSONSerialization dataWithJSONObject:yourNSArray options:NSJSONWritingPrettyPrinted error:&error];
关于ios - iPhone 如何使用 json 发布 nsarry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20878508/