Search code examples
iphonejsonsqlitefmdbnsjsonserialization

Download JSON data via NSJSONSerialization


I have a JSON string from PHP's json_encode(). This is how it looks in JSONViewer.stack.hu, and this is how it looks in the browser.

Is it possible to use NSJSONSerialization to download the JSON data directly? If so, I am going to save the downloaded JSON data to SQLite by using FMDB.


Solution

  • id jsonObjectFromUrlString(NSString *urlString)
    {
        NSURL *url = [NSURL URLWithString:urlString];
        NSError *error = nil;
        id jsonObject = nil;
        NSData *data = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
        if(error)
            NSLog(@"%@", error);
        else
            jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        if(error)
            NSLog(@"%@", error);
        return jsonObject;
    }