Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

How to Create the Mutable Dictionary Dynamically For this Json Response in IOS

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

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
char* jsonCStr = "{             
"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"
    }                           
                     ]          
    }                           
}";

NSString* jsonStr = [NSString stringWithCString:jsonCStr encoding:NSUTF8StringEncoding];
NSData* jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...