我创建了一个简单的PHP文件输出JSON字符串:JSON来的NSDictionary从HTTP POST请求
<?
$test = $_POST["hashcode"];
if ($ttest != "")
{
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'couponcode' => $test);
echo json_encode($arr);
}
?>
我想使用Objective-C语言来检索该JSON并将其解析为一个的NSDictionary。我目前使用JSON框架,但它不适合我。
NSHTTPURLResponse * response;
NSError * error;
NSString *post = @"hashcode=asdf1234fdsa";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"www.ski-inndronten.nl/json.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSData *testJSON = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *jsonString = [testJSON JSONRepresentation];
NSLog(@"String = %@",jsonString);
NSDictionary *testDict = [jsonString JSONValue];
NSLog(@"testDict = %@",testDict);
我希望你能帮助我,因为我不知道我做错了什么。 (我输出NULL对象)
只要放一些断点即可。响应(testJSON数据)在请求后没有任何值。 – Ilanchezhian