NSJSONSerialization 不创建键值对

发布于 2024-12-24 15:51:28 字数 746 浏览 1 评论 0原文

我正在使用 iOS 5 新功能来解析 JSON,但我不知道为什么我没有得到任何键值对。 “aStr”(数据的字符串表示形式)将正确的 JSON 放在输出窗口上,但我在“dicData”中什么也没有得到,也没有错误。

非常感谢任何帮助。

这就是我正在使用的

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

I am using iOS 5 new feature to parse JSON and I have no idea that why I am not getting any key value pairs. "aStr" (string representation of data) is putting the right JSON on the output window but I am getting nothing in "dicData" and there is no error either.

Any help is greatly appreciated.

This is what I am using

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法回应 2024-12-31 15:51:28

您的 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 解析算法,这就是我尝试获取它的方式。

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);
}

Your JSON is formatted this way:

{
  "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",
...

I haven't used NSJSONSerialization but just following the natural JSON parsing alg this is how I would try to get it.

NSDictionary *dicData = [NSJSONSerialization
                           JSONObjectWithData:data
                           options:NSJSONReadingAllowFragments
                           error:&error];

NSDictionary *postData = [dicData objectForKey:@"post"];
NSString *title = [postData objectForKey:@"title"];

EDIT

Just a simple check method:

-(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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文