Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting NSData into NSDictionary

I get the data from an XML file and I am storing it in NSData object. I want to convert that NSData into an NSDictionary and store that data in a plist.

My code is as follows:

NSURL *url = [NSURL URLWithString:@"http://www.fubar.com/sample.xml"];
    NSData *data = [NSData dataWithContentsOfURL:url]; 
    NSLog(@"%@", data);

To convert the data, I am using:

- (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;
}

Can anyone please tell me how to do that?

like image 287
user564963 Avatar asked Aug 18 '11 14:08

user564963


1 Answers

or this:

NSString* dataStr  = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];   
SBJSON *jsonParser = [SBJSON new];
NSDictionary* result = (NSDictionary*)[jsonParser objectWithString:dataStr error:nil];
[jsonParser release];
[dataStr release];
like image 85
Onnmir Avatar answered Oct 04 '22 22:10

Onnmir