0

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.

Jahm
  • 658
  • 1
  • 12
  • 27

2 Answers2

1
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;
}
Minthos
  • 900
  • 4
  • 13
  • Will it be able do download the JSON data in the site? I haven't tested it yet. – Jahm Jan 30 '13 at 16:44
  • Why don't you try it and print out the results? – Minthos Jan 30 '13 at 16:46
  • I was able to output it using NSLog: `( { article = "This is a dummy article about Apple Releases iPad 3"; "date_string" = "1969-12-31 19:00:05"; id = 1; timestamp = 5; title = "Apple Releases iPad 3"; }, {...;}, {...;} )` – Jahm Jan 30 '13 at 17:05
1

JSON Serialization method. please refer this. JSON Serialization

for database did you look at coredata. please have a look at these ones.

add core data to existing xcode project.

A simple intro to use coreData

Community
  • 1
  • 1
medampudi
  • 399
  • 3
  • 15
  • Yeah, I have looked into Core Data. Unfortunately, I haven't fully grasped its idea yet so I'm sticking with FMDB for the mean time. – Jahm Jan 30 '13 at 17:00
  • the problem is simple when you use restkit or something like it you can create models directly that and reduce your processing time for the app to run correctly. – medampudi Jan 30 '13 at 17:03
  • nope...It is id. It takes the form of NSArray if the data you get is an array and NSDictionary if you get just a object. Totally depends that comes up. – medampudi Jan 30 '13 at 17:11
  • Yah, it took the form of NSArray. So, did I downloaded the JSON data? – Jahm Jan 30 '13 at 17:23