0

Web サーバーから JSON オブジェクトを取得するアプリを作成しています。これは、辞書を含む辞書です。これを解析すると、辞書と配列の両方としてすべてが必要になります。これは、TableView を動的に入力するために両方objectForKeyを使用する必要があるためです。objectAtIndex

その一部は次のようになります。

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

構造をドリルダウンするときにインデックスを追加するだけで、1 行に統合できます。

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 関数を使用することをお勧めします。OCTotallyLazy のおかげで、アプリで json データを扱うときの作業がずっと楽になりました。

于 2013-11-13T14:42:35.437 に答える