将带有JSON的NSString转换为NSDictionary

时间:2014-04-25 12:02:38

标签: ios objective-c json nsstring

我有NSString看起来像:

{
   "name": "anEvent",
      "args": [
      {
        "ct": "Un",
        "someUUID": "D7EC06DE-98D3-436F-A657-FB043567FB67",
        "userName": "Joe Smith",
        "long": "-139.724302",
        "lat": "39.402768"
    }
  ]
}

我如何才能获得内在部分

{
    "ct": "Un",
    "someUUID": "D7EC06DE-98D3-436F-A657-FB04383CFB67",
    "userName": "Joe Smith",
    "long": "-139.724305",
    "lat": "39.402768"
}

加入NSDictionary

感谢。

2 个答案:

答案 0 :(得分:5)

反序列化整个字符串,然后使用objectForKey:@"args"钻取到所需的部分。

答案 1 :(得分:3)

尝试使用此代码:

 NSDictionary *dictObj = [NSJSONSerialization JSONObjectWithData:[yourString dataUsingEncoding:NSUTF8StringEncoding]
                                                         options:NSJSONReadingMutableContainers
                                                           error:nil];
 NSDictionary *requiredObject = [[dictObj objectForKey:@"args"] objectAtIndex:0];