2

I am currently working on an iPhone application that takes in data from the following source:

I am trying to figure out how to parse it into a human readable format in say a text field.

My code so far is:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *urlString = [NSString stringWithFormat:@"http://dev.threesixtyapp.com/api/events.php?action=available&id=1"];
    NSURL *url =[NSURL URLWithString:urlString];
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSError  *error;
    NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSLog(@"%@",json);
}

2 Answers 2

2

http://stig.github.com/json-framework/ - SBJson is a great framework for encoding/decoding JSON. I recommend you check it out...It will parse it for you into an NSDictionary, and you simply set the text of the textfield equal to the value in the NSDictionary that you want. It's pretty straightforward using this framework. Your Json should just be a string when you pass it to the SBJson functions btw

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

1 Comment

It's much faster. Even faster than Apple's native plist serialization/deserialization. Check out the speed comparisons on their project page!
2

First of all you have to understand the data structure of your json.
You can use JSON Viewer to view the data structure of your json.
As I can see you are getting array of objects consisting of event_title, date_from and date_to.

NSError *error = nil;
NSArray *jsonArry = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"%@",jsonArry);
for (NSDictionary *dict in jsonArry) {
    NSString * title = [dict objectForKey:@"event_title"];
    NSString * dateTo = [dict objectForKey:@"date_to"];
    NSString * dateFrom = [dict objectForKey:@"date_from"]; 
    NSLog(@"title=%@,dateTo=%@,dateFrom=%@",title,dateTo,dateFrom);
}

1 Comment

@AviramNetanel I have updated my answer. NSError *error = nil;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.