gpt4 book ai didi

iphone - NSDictionary 到 XML

转载 作者:数据小太阳 更新时间:2023-10-29 01:58:37 25 4
gpt4 key购买 nike

我正在尝试将 NSDictionary 转换为 XML。 (我成功地将 NSDictionary 转换为 JSON)。但现在我需要将 NSDictionary 转换为 XML。 Objective-C 中是否有像 JSON 那样的内置序列化器?

int r = arc4random() % 999999999;

//simulate my NSDictionary (to be turned into xml)
NSString *name = [NSString stringWithFormat:@"Posted using iPhone_%d", r];
NSString *stock_no = [NSString stringWithFormat:@"2342_%d", r];
NSString *retail_price = @"12345";

NSArray *keys = [NSArray arrayWithObjects:@"name", @"stock_no", @"retail_price", nil];
NSArray *objects = [NSArray arrayWithObjects:name,stock_no,retail_price, nil];
NSDictionary *theRequestDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

NSDictionary *theFinalRequestDictionary = [NSDictionary dictionaryWithObject:theRequestDictionary forKey:@"product"];

...//其他无关代码省略

NSData *theBodyData = [NSPropertyListSerialization dataFromPropertyList:theFinalRequestDictionary format:NSPropertyListXMLFormat_v1_0   errorDescription:nil];
NSPropertyListFormat format;

id XMLed = [NSPropertyListSerialization propertyListFromData:theBodyData
mutabilityOption:NSPropertyListImmutable
format:&format
errorDescription:nil];

NSLog(@"the XMLed is this: %@", [NSString stringWithFormat:@"%@", XMLed]);

NSLog 不打印 XML 格式的字符串。它像 NSDictionary 一样打印它。我应该使用什么将我的 NSDictionary 序列化为 XML?

最佳答案

propertyListFromData:... 返回一个“属性列表对象”,即根据数据的内容,数组或字典。您真正感兴趣的内容(xml)由 dataFromPropertyList:... 返回,因此存储在您的 theBodyData 变量中。

试试这个:

NSLog(@"XML: %@", [[[NSString alloc] initWithData:theBodyData encoding:NSUTF8StringEncoding] autorelease]);

关于iphone - NSDictionary 到 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9125441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com