1

I have this PHP script here that turns an array into json:

while($row = $result->fetch_row()){
        $array[] = $row;
    }

   echo json_encode($array);

which returns this

[["No","2013-06-08","13:07:00","Toronto","Boston","2013-07-07 17:57:44"]]

Now I trying to display values from this json code into my apps labels. Here is the code from my ViewController.m file:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *strURL = [NSString stringWithFormat:@"http://jamessuske.com/isthedomeopen/isthedomeopenGetData.php"];

    // to execute php code
    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

    // to receive the returend value
    /*NSString *strResult = [[[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding]autorelease];*/


    self.YesOrNow.text = [NSJSONSerialization JSONObjectWithData:dataURL options:0 error:nil];

}

But my label YesOrNow is not displaying anything :( What am I doing wrong?

Do I need to install a JSON library?

3
  • Too many paths to know for sure. You should start by setting a breakpoint in Xcode and ensure you are receiving the JSON string back correctly and debug from there. Commented Jul 8, 2013 at 2:21
  • I am getting this when I put a breakpoint on dataURL - [0] = (Class) <error: unknown Class> Commented Jul 8, 2013 at 3:18
  • @user2499454 I check the above code its working fine. I just tried NSLog(@"Server Data = %@",[NSJSONSerialization JSONObjectWithData:dataURL options:0 error:nil]); and i am getting the data Commented Jul 8, 2013 at 5:05

1 Answer 1

2

You are pretty close, but there are a couple of issues:

  1. You are loading the data, but you're not successfully navigating the results. You are returning an array with one item, which is itself, an array of results. The yes/no text value is the first item of that sub-array.

  2. You shouldn't load data on the main thread. Dispatch that to a background queue, and when updating labels, then dispatch that back to the main queue (as all UI updates must happen on the main queue).

  3. You should check error codes.

Thus, you might end up with something like:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self loadJSON];
}

- (void)loadJSON
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSURL *url = [NSURL URLWithString:@"http://jamessuske.com/isthedomeopen/isthedomeopenGetData.php"];
        NSError *error = nil;
        NSData *data = [NSData dataWithContentsOfURL:url options:0 error:&error];
        if (error)
        {
            NSLog(@"%s: dataWithContentsOfURL error: %@", __FUNCTION__, error);
            return;
        }

        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
        if (error)
        {
            NSLog(@"%s: JSONObjectWithData error: %@", __FUNCTION__, error);
            return;
        }

        NSArray *firstItemArray = array[0];

        NSString *yesNoString = firstItemArray[0];
        NSString *dateString = firstItemArray[1];
        NSString *timeString = firstItemArray[2];
        // etc.

        dispatch_async(dispatch_get_main_queue(), ^{
            self.YesOrNow.text = yesNoString;
        });
    });

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

Comments

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.