iPhone如何用json发布nsarray

时间:2014-01-02 07:44:44

标签: ios iphone json post ios5

我有很多json对象,如:

{"id":"1","list":"456","name":"a"}, {"id":"2","list":"456","name":"b"}, {"id":"3","list":"456","name":"c"}

他们存储在NSDictionary dic1,dic2,dic3中,然后我使用NSArray存储这些dics,

以下是我的问题,如何发布这些词典?

我知道发布一个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 应该是什么?

2 个答案:

答案 0 :(得分:2)

jsonData应为:

jsonData = [NSJSONSerialization dataWithJSONObject:yourNSArray options:NSJSONWritingPrettyPrinted error:&error];

答案 1 :(得分:0)

试试这个 将您的数组转换为json

NSString *sendingData = [yourarray JSONRepresentation];