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
342 views
in Technique[技术] by (71.8m points)

ios - Put NSDictionary data in ModelObject

I receive a JSON object (converted to NSDictionary) which I want to put it into my model object.

i tried this one

  {
        Description = "Desc.";
        EndTime = "2014-06-25T05:35:00";
        Id = "";
        IsActive = 1;
        StartTime = "2014-06-25T05:30:00";
        Title = "Test appointment";
    },
        {
        Description = Qww;
        EndTime = "2014-06-26T02:58:00";
        Id = "";
        IsActive = 1;
        StartTime = "2014-06-26T01:58:00";
        Title = q;
    }

I want to store it into my model object NSobject. but only get first dictioanry

i have tried this one

     MyAppoinmentModel * modelObj;

    modelObj =[[MyAppoinmentModel alloc]init];

modelobj =[[MyAppoinmentModel alloc]init];

    for(NSMutableDictionary * dic in array)
    {

        NSString * strTitle =[dic valueForKey:@"Title"];
        NSString * strDescription =[dic valueForKey:@"Description"];
        NSString * strStartTime =[dic valueForKey:@"StartTime"];
        NSString * strEndTime =[dic valueForKey:@"EndTime"];
        NSString * strMeetingDate =[dic valueForKey:@"MeetingDate"];
        NSString * strIsActive =[dic valueForKey:@"IsActive"];


        modelobj.Titlestr=strTitle;
        modelobj.Descriptionstr=strDescription;
        modelobj.StartTimeStr=strStartTime;
        modelobj.EndTimeStr=strEndTime;
        modelobj.Daystr=strMeetingDate;

    }

Where may i wrong can u help me to do this one.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Here's code that should work. I don't know what the key is that is above the modelObjects (include that next time if you want a code sample response) so I just assumed it's "models".

I'm adding all the modelObj to a NSMutableArray so that you have access to all modelObj afterwords, since there is no guarantee that there will only be one NSDictionary in the JSON return. This uses Hot Licks's suggestion of creating a method to create each MyAppointmentModel.

NOTE: You forgot about the IsActive and ID parts. I've stubbed them in my solution, but you should add them to your code.

  NSArray* jsonSerialArray = [NSJSONSerialization
         JSONObjectWithData:jsonResponse
         options:0
         error:&error];

  NSArray *JSON = [jsonSerialArray objectForKey:@"models"];
  NSMutableArray *modelObjArray = [NSMutableArray array];

  for (NSDictionary *dict in JSON)
  {      
    [modelObjArray addObject:[self createModelObjectWithDictionary:dict]];
  }
  do something with modelObjArray
}

-(MyAppointmentModel *)createModelObjectWithDictionary:(NSDictionary *)dict
{
  MyAppoinmentModel *modelObj = [[MyAppoinmentModel alloc] init];    

  modelobj.Titlestr=[dict valueForKey:@"Title"];
  modelobj.Descriptionstr=[dict valueForKey:@"Description"];
  modelobj.StartTimeStr=[dict valueForKey:@"StartTime"];
  modelobj.EndTimeStr=[dict valueForKey:@"EndTime"];
  modelobj.MeetingDate=[dict valueForKey:@"MeetingDate"];
  modelobj.IsActive=[dict valueForKey:@"IsActive"];
  modelobj.ID=[dict valueForKey:@"ID"];

  return modelobj;
}

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

...