0

我正在制作一个从 Web 服务器获取 JSON 对象的应用程序,它是一个带有字典的字典。当我解析它时,我需要它作为字典和数组,因为我需要同时使用它们objectForKeyobjectAtIndex动态填写我的 TableViews。

其中一些最终看起来像这样:

NSMutableArray *roomsArray = projectDict[@"rooms"];
NSMutableDictionary *roomDict = roomsArray[indexPath.row];
NSMutableArray *tasksArray = roomDict[@"tasks"];
NSMutableDictionary *task = tasksArray[indexPath.row];
NSString *status =  [task objectForKey:@"status"];

因为我需要数组来获取objectAtIndex索引indexPath.row

但是,当我在我的表格视图中单击我UISwitch时,我需要更改 taskStatus。

但我不能,因为它不会“向上工作”。

所以,我需要:

获取 TOP 字典(projectDict)并在 1 行中,(无需按照数组和字典的顺序)替换 taskStatus。

这是我在控制台中打印的 JSON 格式的 projectDict:

projects =     (
            {
        address = "Fugledammen 8";
        city = "S\U00f8borg";
        completedTasks = 0;
        done = 0;
        dueDate = "07/11-2013";
        id = 8;
        rooms =             (
                            {
                id = 9;
                name = Loftet;
                tasks =                     (
                                            {
                        id = 5;
                        name = doSomething;
                        status = 0;
                    }
                );
            },
                            {
                id = 10;
                name = "v\U00e6relse 1";
                tasks =                     (
                                            {
                        id = 6;
                        name = doSomething;
                        status = 0;
                    }
                );
            }
        );
        totalTasks = 2;
        zip = 2860;
    },
            {
        address = "Lygten 37";
        city = "K\U00f8benhavn N";
        completedTasks = 0;
        done = 0;
        dueDate = "06/11-2013";
        id = 6;
        rooms =             (
                            {
                id = 2;
                name = Toilet;
                tasks =                     (
                );
            },
                            {
                id = 3;
                name = Kantine;
                tasks =                     (
                                            {
                        id = 2;
                        name = "S\U00e6tte nyt k\U00f8kken op";
                        status = 0;
                    },
                                            {
                        id = 3;
                        name = "Ops\U00e6tte ny kaffemaskine";
                        status = 0;
                    }
                );
            },
                            {
                id = 4;
                name = "L\U00e6rev\U00e6relse";
                tasks =                     (
                                            {
                        id = 4;
                        name = "Ops\U00e6tte ny tavle";
                        status = 0;
                    }
                );
            }
        );
        totalTasks = 3;
        zip = "";
    },
            {
        address = "Tranedalen 2";
        city = "Ish\U00f8j";
        completedTasks = 0;
        done = 0;
        dueDate = "06/11-2013";
        id = 5;
        rooms =             (
                            {
                id = 1;
                name = "K\U00f8kken";
                tasks =                     (
                );
            },
                            {
                id = 5;
                name = "Badev\U00e6relse";
                tasks =                     (
                );
            },
                            {
                id = 8;
                name = "Sovev\U00e6relse";
                tasks =                     (
                );
            }
        );
        totalTasks = 0;
        zip = 2635;
    },
            {
        address = "L\U00e6rkens Kvt 30c";
        city = Albertslund;
        completedTasks = 1;
        done = 0;
        dueDate = "11/11-2013";
        id = 9;
        rooms =             (
                            {
                id = 11;
                name = "k\U00f8kken";
                tasks =                     (
                                            {
                        id = 7;
                        name = "lav kaffe";
                        status = 1;
                    }
                );
            }
        );
        totalTasks = 1;
        zip = 2620;
    },
            {
        address = "S\U00f8gade 2";
        city = "K\U00f8benhavn K";
        completedTasks = 0;
        done = 0;
        dueDate = "13/11-2013";
        id = 10;
        rooms =             (
        );
        totalTasks = 0;
        zip = 1717;
    }
);
}
4

3 回答 3

0

编码

NSMutableArray *roomsArray = projectDict[@"rooms"];
NSMutableDictionary *roomDict = roomsArray[indexPath.row];
NSMutableArray *tasksArray = roomDict[@"tasks"];
NSMutableDictionary *task = tasksArray[indexPath.row];
NSString *status =  [task objectForKey:@"status"];

可以重新格式化(不需要 Mutable 实例来访问项目)为:

NSArray      *roomsArray = projectDict[@"rooms"];
NSDictionary *roomDict   = roomsArray[indexPath.row];
NSArray      *tasksArray = roomDict[@"tasks"];
NSDictionary *task       = tasksArray[indexPath.row];
NSString     *status     = task[@"status"];

可以通过在向下钻取结构时附加索引来合并为一行。:

NSString *status = projectDict[@"rooms"][indexPath.row][@"tasks"][indexPath.row][@"status"];
于 2013-11-13T14:10:55.463 回答
0

A couple of thoughts:

  1. You are using using indexPath.row for two completely different (and conflicting) purposes, as the index in the array of rooms, as well as the index in the array of tasks. Is your tableview a list of rooms or a list of tasks? It doesn't makes sense for it to be both (e.g. row 0 is first task for first room, row 1 is second task for second room, etc.). I'm going to assume, below, that you have a different section for each room, and each row is for a separate task.

  2. But you've outlined the correct technique for determining the status:

    NSInteger projectIndex = 0;
    NSInteger roomIndex = indexPath.section;
    NSInteger taskIndex = indexPath.row;
    
    NSMutableDictionary *projectDict = projects[projectIndex];
    NSMutableArray *roomsArray = projectDict[@"rooms"];
    NSMutableDictionary *roomDict = roomsArray[roomIndex];
    NSMutableArray *tasksArray = roomDict[@"tasks"];
    NSMutableDictionary *task = tasksArray[taskIndex];
    NSString *status = task[@"status"];
    
    NSLog(@"status = %@", status);
    

    and if you want to change that status (and assuming you used the NSJSONReadingMutableContainers option when you called [NSJSONSerialization JSONObjectWithData:...], you use the same mechanism outlined above, but this time, just set status for task to be the string @"1":

    task[@"status"] = @"1";
    

    Or if status is a NSNumber, it would be:

    task[@"status"] = @1;
    
  3. As an aside, your code snippet suggested that you think status is a NSString. It might be a NSNumber. It's a NSString if the JSON said:

    "status" : "0"
    

    It's a NSNumber if the JSON said:

    "status" : 0
    

    Unfortunately, from your NSLog, we can't tell if it's a NSNumber or NSString. You have to look at the JSON (or look at the class of the status object in your dictionary. Regardless, you don't want to do setValue:0 to set this value. Either use setValue:@"0" or setValue:@0 depending upon whether you're dealing with a string or number, respectively.

Now, I don't know how you're determining the index in the roomsArray and the index for the tasksArray (I'm assuming above that you'll have one section per room, and each row in each section corresponds to the individual tasks), but hopefully this illustrates the idea.

于 2013-11-13T14:59:11.130 回答
0

我推荐使用OCTotallyLazy和 asArray 函数。在我的应用程序中处理 json 数据时,OCTotallyLazy 让我的生活变得更加轻松。

于 2013-11-13T14:42:35.437 回答