Iphone NSJSONSerialization不创建键值对
我正在使用iOS5的新特性来解析JSON,我不知道为什么我没有得到任何键值对。“aStr”(数据的字符串表示)将正确的JSON放在输出窗口上,但我在“dicData”中没有得到任何信息,也没有错误 非常感谢您的帮助 这就是我正在使用的Iphone NSJSONSerialization不创建键值对,iphone,nsdictionary,nsdata,nsjsonserialization,Iphone,Nsdictionary,Nsdata,Nsjsonserialization,我正在使用iOS5的新特性来解析JSON,我不知道为什么我没有得到任何键值对。“aStr”(数据的字符串表示)将正确的JSON放在输出窗口上,但我在“dicData”中没有得到任何信息,也没有错误 非常感谢您的帮助 这就是我正在使用的 NSError *error = nil; NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.macscandal.com/?json
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.macscandal.com/?json=get_post&post_id=436"]];
NSString* aStr;
aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
//NSLog(@"data = %@",aStr);
NSDictionary *dicData = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&error];
//NSLog(@"error = %@",error);
NSString *title = [dicData objectForKey:@"title"];
JSON的格式如下:
{
"status": "ok",
"post": {
"id": 436,
"type": "post",
"slug": "foxconn-likely-to-get-assembly-contract-for-apple-tv-set",
"url": "http:\/\/www.macscandal.com\/index.php\/2011\/12\/28\/foxconn-likely-to-get-assembly-contract-for-apple-tv-set\/",
"status": "publish",
"title": "Foxconn Likely to get Assembly Contract for Apple TV Set",
...
我没有使用过NSJSONSerialization
,但只是遵循自然的JSON解析alg,这就是我试图获得它的方法
NSDictionary *dicData = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&error];
NSDictionary *postData = [dicData objectForKey:@"post"];
NSString *title = [postData objectForKey:@"title"];
编辑
只是一个简单的检查方法:
-(void)check{
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.macscandal.com/?json=get_post&post_id=436"]];
NSDictionary *dicData = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&error];
NSDictionary *postData = [dicData objectForKey:@"post"];
NSString *title = [postData objectForKey:@"title"];
NSLog(@"%@", title);
}
谢谢塞浦路斯人的回答,但我仍然得到0个键/值对?知道吗?不知道为什么会这样。我会试试看。我刚试过,它对我有效:
2011-12-31 20:42:04.595 UIView演示[2406:f803]富士康可能会得到苹果电视机的装配合同
你怎么检查它的价值?真的!!!我正按照你在回答中提到的那样做。你能发布你的代码吗?谢谢