-3

Possible Duplicate:
iPhone/iOS JSON parsing tutorial

I have been reading many tutorials on how to parse JSON data in Objective C but still I am unable to figure it out. I want to parse data from the JSON file and show it up on screen.

For example,

I want to parse data from here and get all the values of different retailers in different variables, so that I can use them later.

How can I do it?

Community
  • 1
  • 1
Samrat Mazumdar
  • 2,641
  • 4
  • 19
  • 28
  • 1
    Take a look at the accepted answer in this question: http://stackoverflow.com/questions/5813077/iphone-ios-json-parsing-tutorial – uldall Oct 21 '12 at 13:53
  • see this: http://stackoverflow.com/questions/10241908/parsing-json-data-in-ios-objective-c-and-displaying-in-tableview-getting-empt – Farshid Oct 21 '12 at 13:57
  • Look for more examples, and if you're having trouble understanding them your understanding of Objective-C is probably insufficient. JSON is not that complicated! – Hot Licks Oct 21 '12 at 18:22
  • (Of course, the example you have in not JSON, which may be one reason why you're not understanding it.) – Hot Licks Oct 21 '12 at 18:23
  • (Let me repeat that.) **THE EXAMPLE YOU HAVE IS NOT VALID JSON SYNTAX!** (It sorta, kinda looks like a dump of an Objective-C NSDictionary, but that shouldn't be served up as a web page.) – Hot Licks Oct 21 '12 at 19:47

2 Answers2

2

Assuming that you have your data in a NSData object you can use NSJSONSerialization class available in iOS 5 and up.

+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error

This is a class method that will convert your data into objects like NSArray, NSDictionary, NSNumber etc. depending on the contents of your data object.

Marcin Kuptel
  • 2,674
  • 1
  • 17
  • 22
1

Here is how you would download and parse the data from a web server. Note that all these methods are part of the same class and there are instance variables named _downloadData of type NSMutableData* and _downloadConnection of type NSURLConnection*. Also note this code assumes ARC is not being used. If it is, just remove the object releases and retains and make sure the instance variables are strong references.

-(void)startDownload {
    NSURL* jsonURL = [NSURL URLWithString:@"http://isbn.net.in/9781449394707.json"];

    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:jsonURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];

    _downloadData = [[NSMutableData dataWithCapacity:512] retain];

    _downloadConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [_downloadData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [_downloadData appendData:data];
}


- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{

   [_downloadConnection release];
   _downloadConnection = nil;

   [_downloadData release];
   _downloadData = nil;
}


- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSError* jsonError = nil;

    NSDictionary* jsonDict = nil; // your data will come out as a NSDictionry from the parser
    jsonDict = [NSJSONSerialization JSONObjectWithData:_downloadData options:NSJSONReadingMutableLeaves  error:&jsonError];


    if ( nil != jsonError ) {
        // do something about the error

        return;
    }

    [_downloadConnection release];
    _downloadConnection = nil;

    [_downloadData release];
    _downloadData = nil;

    // now do whatever you want with your data in the 'jsonDict'
}
kamprath
  • 2,220
  • 1
  • 23
  • 28