Fantasy
Fantasy

Reputation: 251

Can not load a txt to NSDictionary

When I load a txt file in my app, i use the code below:

NSString *txtPath = [mgzPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",mgz]];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:txtPath];

I can make sure file is exist in the path and in the txt is some json string. However i get null value, Can anyone tell me why?

JSON string for reference

{
    "data": {
        "mag_index": {
            "month": 1,
            "id": "1",
            "year": 2013
        },
        "mag_catalog": [
            {
                "id": "8a22e94a45448e0a01454fb8831d03bb",
                "author": "",
                "title": "",
                "image_url": "1397201623655_718.jpg"
            }
        ]
    }
}

Upvotes: 0

Views: 393

Answers (1)

Levi
Levi

Reputation: 7343

You have to parse the content of that JSON, can't just simply read the file content. Do something like:

    NSString *txtPath = [mgzPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",mgz]];
    NSData *JSONData = [NSData dataWithContentsOfFile:txtPath];
    NSError *error = nil;
    NSMutableDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error:&error];

Upvotes: 2

Related Questions