2016-12-06 49 views
0

我是iOS的新手,我面臨着在數組中獲取字典的問題。如何獲得陣列內的NSDictionary

數組是這樣的:

[{"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指數。

+0

您可以像這樣解析字典'for yourArray中的字典'{'dict'是您的解析字典} – PiyushRathi

+0

@PiyushRathi但它只顯示值。 – Muju

+0

我們在這裏申請循環,這將迭代直到數組中的最後一個字典。 – PiyushRathi

回答

0

您可以使用for循環的,如果你想迭代所有Dict

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]; 
+0

我有10個對象在我的array.But當我用你的代碼Dict值打印只有5個鍵/值對。只有一個對象。 – Muju

+0

@Muju你使用For循環? – Dalvik

+0

@Muju cab你發佈了你的實際代碼? – Dalvik

0

你檢查,如果你的JSON字符串結構良好? 「\」用戶1 \「:{\」Iden \「:4,\」nID \「:1},\」user2 \「:{\」Iden \「:5,\」 與「\」 like: 「nID \」:1}}

+0

@MararIbragimov它形成得很好。 – Muju

+0

我的意思是它應該被javascript轉義: –

+0

@「[{\」CheckPointId \「:41,\」AId \「:4,\」CheckPoint \「:\」檢查所有設備和負載附近維護的SOP \ 「,」MId「:41,」MethodOfMeasure「:\」Visual Inspection \「},{\」CheckPointId \「:42,\」AId \「:4,\」CheckPoint \「:\」Check單線圖(SLD)在面板區域\「,\」MId \「:42,\」MethodOfMeasure \「:\」Visual Inspection \「},{\」CheckPointId \「:43,\」AId \ :4,\「CheckPoint \」:\「檢查危險通知在現場維護\」,\「MId \」:43,\「MethodOfMeasure \」:\「Visual Inspection \」}]「 –