iOS JSON NSString Parse [英] iOS JSON NSString Parse
问题描述
我在iOS中有一个JSON字符串作为NSString对象。我想解析这个并在JSON字符串中提取给定的参数。有没有一种有效的方法来解析这个或者是搜索子串等的唯一方法。
I have a JSON string as an NSString object in iOS. I want to parse this and pull out the given parameters in the JSON string. Is there a efficient way to parse this or is the only way to search for substrings etc.?
推荐答案
这样做的方法使用iOS 5将使用 NSJSONSerialization
类。您需要先将字符串转换为NSData对象,然后调用类方法 JSONObjectWithData
The way to do it with iOS 5 is to use the NSJSONSerialization
class. You will want to first convert your string to an NSData object, and call the class method JSONObjectWithData
NSData *jsonData = [myJsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *e;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:nil error:&e];
请注意 JSONObjectWithData
将返回NSDictionary或NSArray,取决于您的JSON字符串是表示字典还是数组。
Note that JSONObjectWithData
will return either an NSDictionary or an NSArray, depending whether your JSON string represents an a dictionary or an array.
这篇关于iOS JSON NSString Parse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!