JSON解析奇数节点

时间:2015-08-06 17:31:59

标签: ios json

我正在通过这个网站解析类似的内容:http://www.prindlepost.org/?json=1

posts: [
{
....
categories: [],
tags: [],
....

'post'中的一个标签是:

author: {
id: 43,
slug: "connergordon_2016",
name: "Conner Gordon",
first_name: "Conner",
last_name: "Gordon",
nickname: "connergordon_2016",
url: "",
description: "Conner is a web and social media intern at the Prindle Institute. A Political Science major from Indiana, Conner's ethical interests lie in memory studies, conflict analysis and the ethics of representation. He also has interests in literature, art and photography."
},

这就是我成功解析其余部分的方法:

// convert to JSON
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];

// extract specific value...
NSArray *results = [res objectForKey:@"posts"];

for (NSDictionary *result in results)
{
   ....
   // loop through the array of categories
    NSArray *categories = [result objectForKey:@"categories"];

    for (NSDictionary *category in categories)
    {
        // ID
        NSString *tempIDCategoryString = [category objectForKey:@"id"];
        NSInteger *UniqueCategoryID = [tempIDCategoryString intValue];

        // slug
        NSString *Categoryslug = [category objectForKey:@"slug"];

        // title
        NSString *Categorytitle = [category objectForKey:@"title"];

        // description
        NSString *Categorydescription = [category objectForKey:@"description"];

        // parent
        NSString *Categoryparent = [category objectForKey:@"parent"];

        // post_count
        NSString *Categorypost_count = [category objectForKey:@"post_count"];
    }
    // </categories>
}

我不知道如何从'post'中的对象数组中解析出这个作者节点。它不是一个数组,它看起来不像我的JSON对象。我可能错了。帮助

2 个答案:

答案 0 :(得分:1)

作者是一个字典节点。你可以试试这个。

 for (NSDictionary *result in results)
{
   ....
   //get the author dictionary
    NSDictionary *postDict = [result objectForKey:@"author"];
   // loop through the array of categories
    NSArray *categories = [result objectForKey:@"categories"];
   ...
 }

答案 1 :(得分:0)

非常简单......只需使用valueForKey

访问它
 NSArray *results = [res objectForKey:@"posts"];
[results[@"author"] valueForKey:@"id"];  

你已经完成了