2013-05-08 62 views
0

如何从下面的JSON检索值?从iOS中的NSDictionary中的字典中检索值

"ItemID": 115, 
    "Name": "Test Items for Receive", 
    "Description": "Recevive", 
    "ItemCode": "AAA-RC-IT1", 
    "Quantity":9000, 
    "WarehouseLocationID": 560, 
    "TransactionDate": "\/Date(-62135596800000+0530)\/", 
    "TransactionTime": "PT11H9M0.6074603S", 
    "Comment": null, 
    "UserName": "virtual", 
    "CustomerItem": { 
    "ItemID": 0, 
    "ItemCode": null, 
    "Description": null, 
    "CustomerID": 10004 
    }, 

我完成了直到UserName,但它不与CustomerItem一起工作。

+0

'的NSDictionary * customerItem = overallDictionary [ “CustomerItem”];' – 2014-02-25 13:02:39

回答

2

dicionaryName [@“key”] [@“key”] [@“key”]等等。如果你可以提供漂亮的印刷JSON,这将是有益的。解析使用NSJSONSerialization类。

编辑:

以获得该项目的ID和

dictionaryName[@"CustomerItem"][@"ItemID"] 

获取商品代码:

dictionaryName[@"CustomerItem"][@"ItemCode"] 

等。

+0

它需要指出的是,有在封闭辞典中的ItemID为好。人们会想要将它们检索到不同名称的变量。 – 2014-02-25 13:05:41

-1
NSDictionary *mainObject = ... 

NSString *userName = mainObject[@"UserName"]; // or [mainObject valueForKey:@"UserName"]; 

NSDictionary *customerItem = mainObject[@"CustomerItem"]; // or [mainObject valueForKey:@"CustomerItem"]; 

NSNumber *itemID = customerItem[@"ItemID"]; // or [customerItem valueForKey:@"ItemID"];