【问题标题】:Put NSDictionary data in ModelObject将 NSDictionary 数据放入 ModelObject
【发布时间】:2014-06-25 19:25:30
【问题描述】:

我收到一个 JSON 对象(转换为 NSDictionary),我想将它放入我的模型对象中。

我试过这个

  {
        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;
    }

我想将它存储到我的模型对象 NSobject 中。但只能得到第一个字典

这个我试过了

     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;

    }

我哪里错了,你能帮我做这个吗?

【问题讨论】:

  • 两个问题。首先,你永远不会在你的 for 循环中创建一个新的modelobj,所以你实际上只会有modelobj = [array lastObject]。其次,您的阵列是否已填充?我们没有关于如何解析 JSON 的代码。
  • 您没有收到 JSON 对象/NSDictionary,您收到的是 JSON 数组/NSArray,包含多个对象/NSDictionary。
  • 我得到了那个 json 数组,现在我想把它放到我的 obj 模型中。我可以这样做吗
  • 迭代数组并创建多个对象,将它们放在一个数组中。
  • 没有。如果你不能处理这个问题,那么你不应该在 Objective-C 中编程。

标签: ios iphone objective-c


【解决方案1】:

这是应该可以工作的代码。我不知道 modelObjects 上方的关键是什么(如果您想要代码示例响应,请下次包括)所以我只是假设它是“模型”。

我将所有modelObj 添加到NSMutableArray 以便您可以访问所有modelObj 后记,因为不能保证JSON 返回中只有一个NSDictionary。这使用了 Hot Licks 的建议,即创建一个方法来创建每个 MyAppointmentModel

注意:您忘记了 IsActive 和 ID 部分。我已经在我的解决方案中将它们存根,但您应该将它们添加到您的代码中。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多