如何在IOS中为此Json响应动态创建Mutable Dictionary [英] How to Create the Mutable Dictionary Dynamically For this Json Response in IOS

查看:110
本文介绍了如何在IOS中为此Json响应动态创建Mutable Dictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的JSON响应1的时间条目Spent_on:2015-12-27

This is my JSON Response 1 for the time entry Spent_on:"2015-12-27"

{
    "A": {
        "user": {
            "id": 1,
            "name": "B"
        },
        "startday": "2015-12-27",
        "status": "New",
        "total": 2,
        "time_entries": [{
            "id": 768,
            "project": {
                "id": 8,
                "name": "C"
            },
            "user": {
                "id": 1,
                "name": "B"
            },
            "activity": {
                "id": 8,
                "name": "D"
            },
            "hours": 2,
            "comments": "",
            "spent_on": "2015-12-27"
        }]
    }
}

我已经创建了这样的字典格式来做帖子操作:

I had created the dictionary format like this to do post operation:

NSDictionary * response =@{@"A": @{@"user": @{@"id": @1,@"name": @"B"},@"startday@": @"2015-12-27",@"status@": @"New",@"total@": 2,@"time_entries@": [{@"id@": 768,@"project@": {@"id@": 8,@"name": @"C"},@"user": {@"id": 1,@"name": @"B"},@"activity": {@"id": 8,@"name": @"D"},@"hours": 2,@"comments": @"",@"spent_on": @"2015-12-27"}]}};

这是我的JSON响应2 for time entry Spent_on:2015-12-27和2015 -12-28

This is my JSON response 2 for time entry Spent_on:"2015-12-27" and "2015-12-28"

{
    "A": {
        "user": {
            "id": 1,
            "name": "B"
        },
        "startday": "2015-12-27",
        "status": "New",
        "total": 6,
        "time_entries": [{
            "id": 768,
            "project": {
                "id": 8,
                "name": "C"
            },
            "user": {
                "id": 1,
                "name": "B"
            },
            "activity": {
                "id": 8,
                "name": "D"
            },
            "hours": 2,
            "comments": "",
            "spent_on": "2015-12-27"
        }, {
            "id": 775,
            "project": {
                "id": 8,
                "name": "C"
            },
            "user": {
                "id": 1,
                "name": "B"
            },
            "activity": {
                "id": 8,
                "name": "D"
            },
            "hours": 4,
            "comments": "",
            "spent_on": "2015-12-28"
        }]
    }
}

json响应2还有一个time_entry id,hours,spend_on than json response1.同样地,对于每个新的日期的每个新的小时条目(paid_on)。新的时间条目id将生成,并且响应长度将增加

The json response 2 having one more time_entry id,hours,spent_on than json response1.similarly, for every new hours entry for every new date(spent_on).A new time entry id will generate and also Response length will increase.

所以,如何创建上述响应的可变字典或字典,并且如果不同的时间和不同的日期会发生(例如一个整周),那么

So, How to create the mutable dictionary or dictionary for above response and also if the different hours and different date will occur(example for one full week).for that how to create dictionary for it?Else any other way to do this.Thanks in advance.

推荐答案

你问:


S o,如何为上述响应创建可变字典或字典?

So, How to create the mutable dictionary or dictionary for above response?

当您使用 JSONObjectWithData ,使用 NSJSONReadingMutableContainers 选项。这将导致可变字典/数组。

When you parse it with JSONObjectWithData, use NSJSONReadingMutableContainers option. That will result in mutable dictionarys/arrays.

NSError *error;
NSMutableDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];




如果不同的时间和不同的日期会发生(例如一个整个星期),为了如何为它创建字典?

also if the different hours and different date will occur(example for one full week).for that how to create dictionary for it?

所以,得到一个可变数组的时间条目的引用:

So, get a reference to that the mutable array of time entries:

NSMutableArray *timeEntries = dictionary[@"A"][@"time_entries"];

然后添加任何您想要的新日期:

And then add whatever you want for the new date:

[timeEntries addObject:@{@"id":       @768,
                         @"project":  @{@"id": @8, @"name": @"C"},
                         @"user":     @{@"id": @1, @"name": @"B"},
                         @"activity": @{@"id": @8, @"name": @"D"},
                         @"hours":    @2,
                         @"comments": @"",
                         @"spent_on": @"2015-12-29"}];

只需重复(或循环)。

这篇关于如何在IOS中为此Json响应动态创建Mutable Dictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆