如何将JSON作为主体发送到请求以代替xml-ios目标c



我之前将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];

现在,我需要为请求发送与JSON相同的主体。我怎样才能做到这一点?

您可以通过创建要发送的数据字典来实现这一点

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];

现在,jsonString是您想要发布的最后一个字符串

最新更新