我在iOS中有一个JSON字符串作为NSString对象。我想解析它并提取JSON字符串中的给定参数。有没有一种有效的方法来解析它,或者是搜索子字符串等的唯一方法?
发布于 2012-04-18 08:18:30
使用iOS 5的方法是使用NSJSONSerialization
类。首先需要将字符串转换为NSData对象,然后调用类方法JSONObjectWithData
NSData *jsonData = [myJsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *e;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:nil error:&e];
请注意,JSONObjectWithData
将返回NSDictionary或NSArray,这取决于您的JSON字符串表示的是字典还是数组。
https://stackoverflow.com/questions/10200790
复制相似问题