2

我有一个从 JSON 得到的字符串。

{
    "Audit_Description": "Request Approved",
    "Module_Name": "Resource Request",
    "Field_DisplayName": null",
    "Previous_Value": Education",
    "Current_Value": Employment",
    "Modified_Timestamp": "08-02-2013"
 },
 {
    "Audit_Description": "Request Approved",
    "Module_Name": "Resource Request",
    "Field_DisplayName": null",
    "Previous_Value": null",
    "Current_Value": null",
    "Modified_Timestamp": "08-02-2013"
}

我想解析数据。从JSON,数据进来NSString如上。我想将它们提取为密钥对值。但我无法解析它们。这应该NSMutableDictionary像键值一样"Audit_Description"转换"Request Approved"

输出:

  @{
      @"Audit_Description" : @"Request Approved",
      @"Module_Name" = @"Resource Request",
      @"Field_DisplayName" : <null>,
      @"Previous_Value" : @"Education",
      @"Current_Value" : @"Employment",
      @"Modified_Timestamp" : @"08-02-2013"
  }

谢谢。

4

1 回答 1

12
NSError *err = nil;
NSArray *arr = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&err];

// access the dictionaries
NSMutableDictionary *dict = arr[0];
for (NSMutableDictionary *dictionary in arr) {
  // do something using dictionary
}

多亏了 NSJSONReadingMutableContainers,这创建了一个可变字典。

于 2013-03-05T12:03:59.657 回答