简体   繁体   English

如何将JSON作为正文发送到请求以代替xml iOS目标C

[英]How to send a JSON as a body to a request in place of a xml ios objective c

I was sending xml file as a body before stated as follows: 我之前将xml文件作为正文发送,如下所示:

[[NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><vs:ClientID version=\"0.4\" xmlns:vs=\"http://www.@@@@@@.com/schemas/store\">%@</vs:ClientID>", openUDID] UTF8String];

Now i need to send the same body as a JSON for the request. 现在,我需要为请求发送与JSON相同的正文。 How can i achieve that? 我该如何实现?

you can do this by creating a dictionary of data you want to send 您可以通过创建要发送的数据字典来做到这一点

NSMutableDictionary *reqDictionary = [[NSMutableDictionary alloc]init];
[reqDictionary setValue:@"yourValue1" forKey:@"YourKey1"];
[reqDictionary setValue:@"yourValue2" forKey:@"YourKey2"];

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:fieldsArray options:NSJSONWritingPrettyPrinted  error:&error];

NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

Now the jsonString is the final string you want to post 现在jsonString是您要发布的最终字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM