我正在尝试解析 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:成功',而是输出'OK:(null)'如果您需要PHP脚本,我可以发布它。