简体   繁体   中英

Parsing JSON returns NULL iOS

I'm trying to parse the JSON string:

array(3) {
  ["result"]=>
  string(7) "success"
  ["source"]=>
  string(12) "setWorldTime"
  ["success"]=>
  bool(true)
}

With this code:

SBJsonParser *parser = [[SBJsonParser alloc] init];

    // Prepare URL request to download statuses from Twitter
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost/con.php?m=setWorldTime&a=London,0"]];

    // Perform request and get JSON back as a NSData object
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    // Get JSON as a NSString from NSData response
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    NSLog(json_string);
    // parse the JSON response into an object
    // Here we're using NSArray since we're parsing an array of JSON status objects
    NSDictionary *statuses = [parser objectWithString:json_string error:nil];
    NSLog(@"OK: %@", [[statuses objectForKey:@"array"] objectForKey:@"result"]);

But instead of output 'OK: success' it outputs 'OK: (null)' If you need the PHP script I can post it.

That string you have is anything but JSON... looks like var_dump() or something from PHP. That's why it can't be parsed.

You need to use json_encode() on your PHP end instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM