-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?

5
  • 1
    Take a look at the accepted answer in this question: stackoverflow.com/questions/5813077/… Commented Oct 21, 2012 at 13:53
  • see this: stackoverflow.com/questions/10241908/… Commented Oct 21, 2012 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! Commented Oct 21, 2012 at 18:22
  • (Of course, the example you have in not JSON, which may be one reason why you're not understanding it.) Commented Oct 21, 2012 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.) Commented Oct 21, 2012 at 19:47

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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'
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.