【问题标题】:iPhone how to post nsarry with jsoniPhone如何使用json发布nsarry
【发布时间】:2014-01-02 07:44:43
【问题描述】:

我有很多 json 对象,例如:

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

他们存储在NSDictionarydic1、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 应该是什么?

【问题讨论】:

  • 请看下面的答案。这与您想要的非常相似。 stackoverflow.com/questions/10290767/…
  • 我有一个使用 xml 标签结构的解决方案,但我无法在答案中显示它,所以请使用这种方式试试这个格式
    1ascharan
    2ascharan
    这里为
    2ascharan
    运行一个循环,并将变量分配给 id、name 和 list在这里有一个带有信息列表的 xml 结构

标签: ios iphone json post ios5


【解决方案1】:

jsonData 应该是:

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

【讨论】:

    【解决方案2】:

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

    NSString *sendingData = [yourarray JSONRepresentation];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-29
      • 2021-03-17
      • 2015-05-05
      • 2018-05-28
      • 2011-02-24
      • 2011-09-09
      相关资源
      最近更新 更多