Ios 将NSData转换为NSDictionary

Ios 将NSData转换为NSDictionary,ios,xml,nsdictionary,nsdata,Ios,Xml,Nsdictionary,Nsdata,我从XML文件中获取数据,并将其存储在NSData对象中。我想将该NSData转换为NSDictionary,并将该数据存储在plist中 我的代码如下: NSURL *url = [NSURL URLWithString:@"http://www.fubar.com/sample.xml"]; NSData *data = [NSData dataWithContentsOfURL:url]; NSLog(@"%@", data); 要转换数据,我将使用: - (NSDic

我从XML文件中获取数据,并将其存储在NSData对象中。我想将该NSData转换为NSDictionary,并将该数据存储在plist中

我的代码如下:

NSURL *url = [NSURL URLWithString:@"http://www.fubar.com/sample.xml"];
    NSData *data = [NSData dataWithContentsOfURL:url]; 
    NSLog(@"%@", data);
要转换数据,我将使用:

- (NSDictionary *)downloadPlist:(NSString *)url {
    NSMutableURLRequest *request  = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10]; 
    NSURLResponse *resp = nil;
    NSError *err = nil;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&resp error:&err];

    if (!err) {
        NSString *errorDescription = nil;
        NSPropertyListFormat format;
        NSDictionary *samplePlist = [NSPropertyListSerialization propertyListFromData:responseData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&errorDescription];

        if (!errorDescription)
            return samplePlist;

        [errorDescription release];              
    }

    return nil;
}
有人能告诉我怎么做吗?

试试下面的代码:

NSString *newStr1 = [[NSString alloc] initWithData:theData1 encoding:NSUTF8StringEncoding];
NSString *newStr2 = [[NSString alloc] initWithData:theData2 encoding:NSUTF8StringEncoding];
NSString *newStr3 = [[NSString alloc] initWithData:theData3 encoding:NSUTF8StringEncoding];

NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", @"key3", nil];
NSArray *objects = [NSArray arrayWithObjects:newStr1 , newStr2 , newStr3 , nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

for (id key in dictionary) {
    NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]);
}

NSString *path = [[NSBundle mainBundle] pathForResource:@"Login" ofType:@"plist"]; 
[dictionary writeToFile:path  atomically:YES];
//here Login is the plist name.
快乐编码

或者:

NSString* dataStr  = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];   
SBJSON *jsonParser = [SBJSON new];
NSDictionary* result = (NSDictionary*)[jsonParser objectWithString:dataStr error:nil];
[jsonParser release];
[dataStr release];

嗯,您需要读取XML文件并提取属于词典的XML元素。如何操作取决于XML文件的结构。这只适用于JSON数据,问题是关于XML数据的。