【问题标题】:How to get the NSDictionary inside Array如何在数组中获取 NSDictionary
【发布时间】:2016-12-06 06:36:22
【问题描述】:

我是 iOS 新手,我在将字典放入数组时遇到问题。

数组是这样的:

[{"CheckPointId":41,"AId":4,"CheckPoint":"Check SOP maintained near all equipments and loads","MId":41,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":42,"AId":4,"CheckPoint":"Check Single line diagram (SLD) is maintained in panel area","MId":42,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":43,"AId":4,"CheckPoint":"Check danger notices are maintained at site","MId":43,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":44,"AId":4,"CheckPoint":"Check Rubber mats are maintained infront of the electrical panels","MId":44,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":45,"AId":4,"CheckPoint":"Check the availibility of HT gloves","MId":45,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":46,"AId":4,"CheckPoint":"Check Treatment of Electric shock board is displayed","MId":46,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":47,"AId":4,"CheckPoint":"Check Total connected load details are displayed","MId":47,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":48,"AId":4,"CheckPoint":"Check LOTO is in practice ","MId":48,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":49,"AId":4,"CheckPoint":"Check for Lightning arrestor installed in terrace","MId":49,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":50,"AId":4,"CheckPoint":"Check APFC is maintaining power factor (0.99 - 1.00)","MId":50,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":51,"AId":4,"CheckPoint":"Check for history cards maintained near equipments","MId":51,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":52,"AId":4,"CheckPoint":"Check checklists are maintained up to date and duely signed by site incharge","MId":52,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":53,"AId":4,"CheckPoint":"Check for earthing provided to equipments","MId":53,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":54,"AId":4,"CheckPoint":"Check for exhaust mechansim in the equipment and load section","MId":54,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":55,"AId":4,"CheckPoint":"Check for fire extinguisher in the equipment area","MId":55,"MethodOfMeasure":"Visual Inspection"}]

我用过这样的代码

NSString *string = [LoginResult valueForKey:@"text"];

NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];

NSArray *responsearry = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

for(NSDictionary *dict in responsearry) {
    NSLog(@"----dict--%@",dict);
}

但它只给了我在0th 索引上的第一个值

【问题讨论】:

  • 你可以像这样解析字典for dict in yourArray{dict是你解析的字典}
  • @PiyushRathi 但它只显示价值。
  • 我们在这里申请了for循环,这将迭代到你数组中的最后一个dict。
  • 您的代码是正确的,只需检查您的数组的计数。计数是 1 吗?
  • @PiyushRathi 它的计数是 10 个对象。

标签: objective-c nsarray nsdictionary


【解决方案1】:

如果你想迭代所有Dict,你可以使用for循环:

    NSArray *yourArray = [{"CheckPointId":41,"AId":4,"CheckPoint":"Check SOP maintained near all equipments and loads","MId":41,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":42,"AId":4,"CheckPoint":"Check Single line diagram (SLD) is maintained in panel area","MId":42,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":43,"AId":4,"CheckPoint":"Check danger notices are maintained at site","MId":43,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":44,"AId":4,"CheckPoint":"Check Rubber mats are maintained infront of the electrical panels","MId":44,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":45,"AId":4,"CheckPoint":"Check the availibility of HT gloves","MId":45,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":46,"AId":4,"CheckPoint":"Check Treatment of Electric shock board is displayed","MId":46,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":47,"AId":4,"CheckPoint":"Check Total connected load details are displayed","MId":47,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":48,"AId":4,"CheckPoint":"Check LOTO is in practice ","MId":48,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":49,"AId":4,"CheckPoint":"Check for Lightning arrestor installed in terrace","MId":49,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":50,"AId":4,"CheckPoint":"Check APFC is maintaining power factor (0.99 - 1.00)","MId":50,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":51,"AId":4,"CheckPoint":"Check for history cards maintained near equipments","MId":51,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":52,"AId":4,"CheckPoint":"Check checklists are maintained up to date and duely signed by site incharge","MId":52,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":53,"AId":4,"CheckPoint":"Check for earthing provided to equipments","MId":53,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":54,"AId":4,"CheckPoint":"Check for exhaust mechansim in the equipment and load section","MId":54,"MethodOfMeasure":"Visual Inspection"},{"CheckPointId":55,"AId":4,"CheckPoint":"Check for fire extinguisher in the equipment area","MId":55,"MethodOfMeasure":"Visual Inspection"}];

     for(NSDictionary *dict in yourArray)
     {
          NSLog(@"----dict--%@",dict);
     }

如果您想在特定索引处获得Dictionary

  int index = 1;
  NSDictionary *dict = [yourArray objectAtIndex:index];

【讨论】:

  • 我的数组中有 10 个对象。但是当我使用您的代码时,Dict 值仅打印 5 个键/值对。意味着只有一个对象。
  • @Muju 你在用 For 循环吗??
  • 是的,我正在使用 for 循环。
  • @Muju NSLog 你的responsearry[responsearry count] 并向我们展示输出。
  • 它的计数是 15 个对象。
【解决方案2】:

您是否检查过您的 json 字符串格式是否正确? 和 ”\” 像 : {\"user1\":{\"Iden\":4,\"nID\":1},\"user2\":{\"Iden\":5,\"nID\":1}}

【讨论】:

  • 我的意思是它应该被 javascript 转义:
  • @"[{\"CheckPointId\":41,\"AId\":4,\"CheckPoint\":\"检查所有设备和负载附近维护的 SOP\",\"MId \":41,\"MethodOfMeasure\":\"Visual Inspection\"},{\"CheckPointId\":42,\"AId\":4,\"CheckPoint\":\"检查单线图(SLD ) 维护在面板区域\",\"MId\":42,\"MethodOfMeasure\":\"Visual Inspection\"},{\"CheckPointId\":43,\"AId\":4,\" CheckPoint\":\"现场维护检查危险通知\",\"MId\":43,\"MethodOfMeasure\":\"Visual Inspection\"}]"
  • 我已经缩短了 json 字符串,因为要评论的字符有限制,但我尝试使用转义格式对整个字符串进行脱轨,效果很好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-20
  • 2016-04-13
  • 1970-01-01
相关资源
最近更新 更多