解析JSON返回NULL iOS


Parsing JSON returns NULL iOS

我正在尝试解析JSON字符串:

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

使用此代码:

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"]);

但它不是输出"OK:success",而是输出"OK:(null)"如果你需要PHP脚本,我可以发布它。

您拥有的字符串不是JSON。。。看起来像var_dump()或PHP中的东西。这就是为什么它不能被解析。

您需要在PHP端使用json_encode()