如何将JSON数据对象发布到服务器iOS8?

时间:2015-06-01 10:43:10

标签: objective-c json

我想使用JSON数据类型的POST方法将在iOS上创建的新对象发送到接收服务器。据我所知,在iOS中从服务器接收数据时,所有JSON处理都是由Apple引入iOS 8简化的。但与GETting JSON对象相比,POST这些并不是我能找到的任何地方。

我尝试解决问题的第一步看起来如下:

如何将以下格式发送到服务器???

 {"createFrom":"","createType":"","filename":"AC","filter":"","lstData":[{"FieldName":"LNK_RELATED_CN","FieldValue":""},{"FieldName":"LNK_RELATED_CO","FieldValue":""},{"FieldName":"MLS_PURPOSE","FieldValue":"Inquiry"},{"FieldName":"MLS_STATUS","FieldValue":"Open"},{"FieldName":"MMO_NOTES","FieldValue":""},{"FieldName":"DTE_NEXTACTIONDATE","FieldValue":""},{"FieldName":"MMO_NEXTACTION","FieldValue":""}],"password":"Infodat2","username":"manmeets","IsNew":true}

我见过这样的代码:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"%@AssetSave",[defaults objectForKey:@"siteAddress"]]];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:jsonData];
_responseData = [NSMutableData data];
NSLog(@"request : %@", request);
_nsurlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

但我真的不知道如何使用POST方法将JSON对象发送到服务器。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

您拥有的代码很好,您只需要创建jsonData

jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];

(以为你真的应该包括一个&error,这样你就可以看到出现问题时会发生什么事情)