在iOS中进行JSON解析,给我" null"

时间:2014-05-01 10:56:09

标签: ios json

我有这个JSON,我想解析。

"entry"[
        {"id" { },
        "published" { },
        "content"   { },
        "link" [5], {       "rel": "something",
                            "type":"other stuff",
                            "href":"http://www....",
                            "title":"TITLE..",
                            "thing":"something"
                    }
        },

        {"id" { },
        "published" { },
        "content"   { },
        "link" [5], {       "rel": "something",
                            "type":"other stuff",
                            "href":"http://www....",
                            "title":"TITLE..",
                            "thing":"something"

                    }
        .......
        }


    ]

我做这样的事情:

NSDictionary* dataDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSMutableArray *NewsArrayTMP = dataDictionary[@"feed"][@"entry"];

for (NSDictionary *vDictionary in NewsArrayTMP)
{
    News *news = [[News alloc] init];

    news.publishedNews= [vDictionary valueForKeyPath:@"published.$t"];
    news.updatedNews= [vDictionary valueForKeyPath:@"updated.$t"];
    news.contentNews= [vDictionary valueForKeyPath:@"content.$t"];   
    news.linkForShare= [vDictionary valueForKeyPath:@"link.href"];

    [self.Newsrray addObject:news];
}

通过这种方式,我得到了所有的" href"在数组中。例如,我会得到第4个。

我也尝试过这个:

news.linkForShare= [vDictionary valueForKeyPath:@"link.href.[4]"];

但字符串为null

我也试过这个:

news.linkForShare= [vDictionary valueForKeyPath:@"link.href.4"];

但我错了。我怎么能这样做?

3 个答案:

答案 0 :(得分:0)

JSON需要:在键和值之间:所以你需要通过你的JSON并像这样修改它

{
  "entry": [
     {
       "id": {}
      }
  ]
}

等等。您可以通过JSONLint运行JSON来帮助您验证它。

另外,您不需要[5]

答案 1 :(得分:0)

尝试将您的dataDictionary作为NSArray而不是NSDictionary。 你应该将你的词典作为数组的元素。

NSArray * dataArray = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions错误:& error];

答案 2 :(得分:0)

这是json:

{

" feed":{

"entry": [
  {
    "id": {
      "$t": "tag:blogger.com,1999:blog-64444540.post-341089429"
    },
    "published": {
      "$t": "2014-05-01T09:00:00.000+02:00"
    },
    "updated": {
      "$t": "2014-05-01T09:00:07.017+02:00"
    },
    "title": {
      "type": "text",
      "$t": "Bocce \" rinforzo giusto”"
    },
    "content": {
      "type": "html",
      "$t": "<div class=\"MsoNormal\"><br /></div><div class=\"MsoNormal\"><span style=\"font-family: Arial, Helvetica, sans-serif;\"><br /></span></div><div class=\"separator\" style=\"clear: both; text-align: center;\"><a href=\"http://1.bp.blogspot.com/-\" imageanchor=\"1\" style=\"clear: left; float: left; margin-bottom: 1em; margin-right: 1em;\"><img border=\"0\" src=\"celvetica, sans-serif;\"><b><i><br /></i></b></span></div><div class=\"MsoNormal\"><span style=\"font-family: Arial, Helvetica, sans-serif;\"></span></div><div class=\"MsoNormal\"><span style=\"font-family: Arial, Helvetica, sans-serif;\"><br /></span></div><div class=\"MsoNormal\"><span style=\"font-family: Arial, Helvetica, sans-serif;\">” </i></b></span><br /><div class=\"MsoNormal\"><span style=\"font-family: Arial, Helvetica, sans-serif;\"><b><i><a href=\"httpsa></i></b></span></div><div class=\"MsoNormal\"><span style=\"font-family: Arial, Helvetica, sans-serif;\"><b><br /></b></span></div><div class=\"MsoNormal\"><span style=\"font-family: Arial, Helvetica, sans-serif;\">></span></div><br /><div class=\"MsoNormal\"><br /></div>"
    },
    "link": [
      {
        "rel": "replies",
        "type": "application/atom+xml",
        "href": "http://test.blogspot.com/feeds/45432259045304590/comments/default",
        "title": "Comments"
      },
      {
        "rel": "replies",
        "type": "text/html",
        "href": "http://test.blogspot.com/feeds/45432259045304590/comments/default.html#comment-form",
        "title": "0 Commenti"
      },
      {
        "rel": "edit",
        "type": "application/atom+xml",
        "href": "ttp://test.blogspot.com/feeds/45432259045304590/comments/default/45432259045304590"
      },
      {
        "rel": "self",
        "type": "application/atom+xml",
        "href": "http://www.blogger.com/feeds/32432432442/posts/default/534464793232329"
      },
      {
        "rel": "alternate",
        "type": "text/html",
        "href": "http://test.blogspot.com/2014/05/testpaginadacercare.html",
        "title": "Bocce ”"
      }
    ],
    "author": [
      {
        "name": {
          "$t": "IO"
        },
        "uri": {
          "$t": "http://www.blogger.com/profile/0232131232132321"
        },
        "email": {
          "$t": "noreply@blogger.com"
        },
        "gd$image": {
          "rel": "http://schemas.google.com/g/2005#thumbnail",
          "width": "32",
          "height": "32",
          "src": "http://4.bp.blogspot.com/3442/3434435ff/hhhhf4435345/ffd44-p0/s1000/*"
        }
      }
    ],

    "thr$total": {
      "$t": "0"
    }
  },
  {
    "id": {
      "$t": "tag:blogger.com,1999:blog-64444540.post-341089429"
    },
    "published": {
      "$t": "2014-05-01T09:00:00.000+02:00"
    },
    "updated": {
      "$t": "2014-05-01T09:00:07.017+02:00"
    },
    "title": {
      "type": "text",
      "$t": "Bocce \" rinforzo giusto”"
    },
    "content": {
      "type": "html",
      "$t": "<div class=\"MsoNormal\"><br /></div><div class=\"MsoNormal\"><span style=\"font-family: Arial, Helvetica, sans-serif;\"><br /></span></div><div class=\"separator\" style=\"clear: both; text-align: center;\"><a href=\"http://1.bp.blogspot.com/-\" imageanchor=\"1\" style=\"clear: left; float: left; margin-bottom: 1em; margin-right: 1em;\"><img border=\"0\" src=\"celvetica, sans-serif;\"><b><i><br /></i></b></span></div><div class=\"MsoNormal\"><span style=\"font-family: Arial, Helvetica, sans-serif;\"></span></div><div class=\"MsoNormal\"><span style=\"font-family: Arial, Helvetica, sans-serif;\"><br /></span></div><div class=\"MsoNormal\"><span style=\"font-family: Arial, Helvetica, sans-serif;\">” </i></b></span><br /><div class=\"MsoNormal\"><span style=\"font-family: Arial, Helvetica, sans-serif;\"><b><i><a href=\"httpsa></i></b></span></div><div class=\"MsoNormal\"><span style=\"font-family: Arial, Helvetica, sans-serif;\"><b><br /></b></span></div><div class=\"MsoNormal\"><span style=\"font-family: Arial, Helvetica, sans-serif;\">></span></div><br /><div class=\"MsoNormal\"><br /></div>"
    },
    "link": [
      {
        "rel": "replies",
        "type": "application/atom+xml",
        "href": "http://test.blogspot.com/feeds/45432259045304590/comments/default",
        "title": "Comments"
      },
      {
        "rel": "replies",
        "type": "text/html",
        "href": "http://test.blogspot.com/feeds/45432259045304590/comments/default.html#comment-form",
        "title": "0 Commenti"
      },
      {
        "rel": "edit",
        "type": "application/atom+xml",
        "href": "ttp://test.blogspot.com/feeds/45432259045304590/comments/default/45432259045304590"
      },
      {
        "rel": "self",
        "type": "application/atom+xml",
        "href": "http://www.blogger.com/feeds/32432432442/posts/default/534464793232329"
      },
      {
        "rel": "alternate",
        "type": "text/html",
        "href": "http://test.blogspot.com/2014/05/testpaginadacercare.html",
        "title": "Bocce ”"
      }
    ],
    "author": [
      {
        "name": {
          "$t": "IO"
        },
        "uri": {
          "$t": "http://www.blogger.com/profile/0232131232132321"
        },
        "email": {
          "$t": "noreply@blogger.com"
        },
        "gd$image": {
          "rel": "http://schemas.google.com/g/2005#thumbnail",
          "width": "32",
          "height": "32",
          "src": "http://4.bp.blogspot.com/3442/3434435ff/hhhhf4435345/ffd44-p0/s1000/*"
        }
      }
    ],

    "thr$total": {
      "$t": "0"
    }
  },
  {
    "id": {
      "$t": "tag:blogger.com,1999:blog-64444540.post-341089429"
    },
    "published": {
      "$t": "2014-05-01T09:00:00.000+02:00"
    },
    "updated": {
      "$t": "2014-05-01T09:00:07.017+02:00"
    },
    "title": {
      "type": "text",
      "$t": "Bocce \" rinforzo giusto”"
    },
    "content": {
      "type": "html",
      "$t": "<div class=\"MsoNormal\"><br /></div><div class=\"MsoNormal\"><span style=\"font-family: Arial, Helvetica, sans-serif;\"><br /></span></div><div class=\"separator\" style=\"clear: both; text-align: center;\"><a href=\"http://1.bp.blogspot.com/-\" imageanchor=\"1\" style=\"clear: left; float: left; margin-bottom: 1em; margin-right: 1em;\"><img border=\"0\" src=\"celvetica, sans-serif;\"><b><i><br /></i></b></span></div><div class=\"MsoNormal\"><span style=\"font-family: Arial, Helvetica, sans-serif;\"></span></div><div class=\"MsoNormal\"><span style=\"font-family: Arial, Helvetica, sans-serif;\"><br /></span></div><div class=\"MsoNormal\"><span style=\"font-family: Arial, Helvetica, sans-serif;\">” </i></b></span><br /><div class=\"MsoNormal\"><span style=\"font-family: Arial, Helvetica, sans-serif;\"><b><i><a href=\"httpsa></i></b></span></div><div class=\"MsoNormal\"><span style=\"font-family: Arial, Helvetica, sans-serif;\"><b><br /></b></span></div><div class=\"MsoNormal\"><span style=\"font-family: Arial, Helvetica, sans-serif;\">></span></div><br /><div class=\"MsoNormal\"><br /></div>"
    },
    "link": [
      {
        "rel": "replies",
        "type": "application/atom+xml",
        "href": "http://test.blogspot.com/feeds/45432259045304590/comments/default",
        "title": "Comments"
      },
      {
        "rel": "replies",
        "type": "text/html",
        "href": "http://test.blogspot.com/feeds/45432259045304590/comments/default.html#comment-form",
        "title": "0 Commenti"
      },
      {
        "rel": "edit",
        "type": "application/atom+xml",
        "href": "ttp://test.blogspot.com/feeds/45432259045304590/comments/default/45432259045304590"
      },
      {
        "rel": "self",
        "type": "application/atom+xml",
        "href": "http://www.blogger.com/feeds/32432432442/posts/default/534464793232329"
      },
      {
        "rel": "alternate",
        "type": "text/html",
        "href": "http://test.blogspot.com/2014/05/testpaginadacercare.html",
        "title": "Bocce ”"
      }
    ],
    "author": [
      {
        "name": {
          "$t": "IO"
        },
        "uri": {
          "$t": "http://www.blogger.com/profile/0232131232132321"
        },
        "email": {
          "$t": "noreply@blogger.com"
        },
        "gd$image": {
          "rel": "http://schemas.google.com/g/2005#thumbnail",
          "width": "32",
          "height": "32",
          "src": "http://4.bp.blogspot.com/3442/3434435ff/hhhhf4435345/ffd44-p0/s1000/*"
        }
      }
    ],

    "thr$total": {
      "$t": "0"
    }
  }
]

} }

现在我发布有效的代码以及如何获取日期:

NSMutableArray *NewsArrayTMP = dataDictionary[@"feed"][@"entry"];

for ( NSDictionary * vDictionary in NewsArrayTMP)
{

    News *news= [[News alloc]init];

    news.titleNews= [vDictionary valueForKeyPath:@"title.$t"];
    news.dNews= [vDictionary valueForKeyPath:@"id.$t"];
    news.publishedNews= [vDictionary valueForKeyPath:@"published.$t"];
    news.updatedNews= [vDictionary valueForKeyPath:@"updated.$t"];
    newsBlogSpot.contentNews= [vDictionary valueForKeyPath:@"content.$t"];

        ////// HERE THE PROBLEM
    newsBlogSpot.linkForShare= [vDictionary valueForKeyPath:@"link.href"];

问题出在最后一行,因为我想得到&#34; href:&#34;第四个&#34;链接:&#34;

和我一样,我得到了&#34; href:&#34;所有&#34;链接:&#34;。 我希望我已经清楚了。 感谢