iOS中的NSArray问题


NSArray issue in iOS

我正在将数组对象发送到php服务器。我不知道如何将数组格式的数据发送到php服务器

当我尝试发送数组格式时,我的数组格式看起来像。如何去除黄铜和双引号

"(
vdsvvdsvdsv
)"

请让我知道我怎样才能把它做好。

此行

[request setHTTPBody:[[NSString stringWithFormat:@"userid=%@&projectSponsor=%@&projectid=%@", userId,addSponsorEmailStringInModel,projectId] dataUsingEncoding:NSUTF8StringEncoding]];

实际上应该是两部分

NSString *emails = [addSponsorEmailStringInModel componentsJoinedByString:@","];
[request setHTTPBody:[[NSString stringWithFormat:@"userid=%@&projectSponsor=%@&projectid=%@", userId,emails,projectId] dataUsingEncoding:NSUTF8StringEncoding]];

这样您就可以显式地将数组转换为服务器所期望的字符串格式

在将NSArray发送到服务器之前,需要先将其转换为NSData,然后再转换为JSON字符串。

希望此链接能帮助:https://stackoverflow.com/a/10323594/3336675

编辑:

NSMutableString *jsonString = [[NSMutableString alloc]initWithString:@"{'"userId'":'""];
[jsonString appendFormat:@"%@'",'"projectSponsor'":'"%@'",'"projectId'":'"%@'"}", userId, addSponsorEmailStringInModel, projectId];

然后

NSString *urlString = [NSString stringWithFormat:@"%@",@"http://url.com/..."];
NSMutableData *responseData;
NSString* escapedUrlString =[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* serverUrl = [NSURL URLWithString:escapedUrlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:serverUrl];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; //set value according to your requirement

然后

[request setHTTPBody:[NSString stringWithString:jsonString] dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:@"POST"];

然后,如果你想要返回值,使用这个

NSURLResponse *res = nil;
NSError *err = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&res error:&err];//or asynchronous, as you wish

data现在保存返回的JSON对象

它取决于为处理输入流而编写的服务器代码。

基本上,服务器传输需要XML或JSON数据。

对于JSON,选择一个简单的解析器,比如SBJSONParser,并将数组传递给它

对于XML,您需要使用NSDATA将数据转换为字节,然后将其传递到标记中。