从Objective C中的json Object获取值?

时间:2013-05-22 06:52:21

标签: ios objective-c json cocoa-touch

我正在使用目标c,这是我的问题..

{
    "STATUS":"OK",
    "projects":
    [

    {

        "company":
        {
            "name":"ABC Pvt Ltd.","id":"12345"
        },
        "created-on":"2012-07-07T04:29:29Z",
        "category":
        {
            "name":"",
            "id":""
        },
        "starred":false,
        "name":"MY Platform 1",
        "startDate":"",
        "logo":"abc.png","notifyeveryone":false,
        "id":"70596",
        "last-changed-on":"2013-05-20T12:22:11Z",
        "status":"active",
        "endDate":""

    },

    {   
        "company":{
                    "name":"ABC Pvt Ltd.",
                    "id":"31222"
                   },
        "created-on":"2012-05-22T07:06:30Z",
        "category":{
                    "name":"","id":""
                    },
        "starred":false,
        "name":"Miscellaneous 1",
        "startDate":"",
        "logo":"abc.png",
        "notifyeveryone":false,
        "id":"12345",
        "last-changed-on":"2013-05-20T12:19:45Z",
        "status":"active",
        "endDate":""
    }
    ]
    }

上面是我的json字符串,正在从asihttpRequest

中检索

现在我想显示项目名称,例如.MY Platform 1和Miscellaneous 1

我刚刚获得了以下项目数组:

NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData:data //1

                          options:kNilOptions
                          error:&error];



   ProjectDetailsarray = [json objectForKey:"projects"];

现在任何人都可以让我知道我如何得到项目的名称,正如我在上面的表视图中提到的两行。

首先是我的平台1 第二个是杂项1

我是目标c的新手所以我试图解决但是ddint找到了获得项目名称的方法吗?

6 个答案:

答案 0 :(得分:4)

简单,遍历数组:

for (NSDictionary *project in json[@"projects"]) {
    NSLog(@"Project name: %@", project[@"name"]);
}

注意我使用的是下标符号,而不是[json objectForKey:@"projects"],这要短得多。

答案 1 :(得分:2)

你有一系列词典。 如何从字典中获取您应该知道的值:

NSDictionary *dic = @{@"Name": @"Andrew", 
                      @"Surname": @"Kama", 
                      @"Age": @24, 
                      @"Friends":@[@"Lesly", @"John", @"Carter"]};

NSString *name = dic[@"Name"];

获取第二个朋友的名字:

NSArray *friends = dic[@"Friends"];
NSString *secondFriend = friends[1]; // or dic[@"Friends"][1];

等等。

您可以将JSON视为嵌套的NSDictionaries / NSArrays。

迭代项目:

NSArray *projects = json[@"projects"];
for (NSDictionary *project in projects) {
    NSLog(@"Project name: %@", project[@"name"]);

    // how to get company name?
    // project[@"company"][@"name"];
    // etc
}

答案 2 :(得分:1)

因为项目是一个数组,你可以枚举数组并通过其键获取项目名称,如下所示

__block NSMutableArray *projectNameArray=[[NSMutableArray alloc] init];
NSArray *projects = [json objectForKey:@"projects"];

[projects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSLog(@"project %i %@",idx,[obj valueForKey:@"name"]);
    [projectNameArray addObject:[obj valueForKey:@"name"]];
}];

NSLog(@"project name array is %@",projectNameArray);

答案 3 :(得分:1)

您可以使用Key-Value Coding使用以下代码获取项目名称数组:

NSArray *projectsNames = [json valueForKeyPath:@"projects.name"];

此示例代码:

NSArray *projectsNames = [json valueForKeyPath:@"projects.name"];
NSLog(@"Projects names: %@", projectsNames);

给出这个输出:

Projects names: (
  "MY Platform 1",
  "Miscellaneous 1"
)

您可以在此处获取有关文档中键值编码的更多信息:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueCoding/Articles/BasicPrinciples.html

答案 4 :(得分:0)

你在json中有一系列项目。那个数组包含字典。

所以基本上首先你需要从数组中获取字典,然后你可以从字典中获取价值。

喜欢这个..

NSArray *projects = [json objectForKey:@"projects"];
for(NSDictionary *dict in projects)
    NSLog(@"Project Name: %@",[dict objectForKey:@"name"]);

答案 5 :(得分:0)

这很简单。只需从您的数组中获取这样的值:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

   static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }

    cell.textLabel.text = [[ProjectDetailsarray objectAtIndex:indexPath.row] objectForKey:@"name"];
}