我是iPhone编程的新手。任何人都可以告诉我如何解析iPhone中的JSON字符串? 我在我的应用程序中使用JSON解析。这是我的JSON数据: JSON格式是dz。
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
}
}
我该如何解析?
答案 0 :(得分:1)
你可以使用一些JSON-Framework,即 https://github.com/stig/json-framework
答案 1 :(得分:0)
另一种解决方案是NSRegularExpression
将json数据保存在字符串中,然后使用正则表达式
例如,第一行的正则表达式
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\"firstName\":[^\"]*\"([^\"]*)\"" options:0 error:&error];
NSArray *matches = [regex matchesInString:theString options:0 range:NSMakeRange(0, [theString length])];
NSTextCheckingResult *match = [matches objectAtIndex:0];
NSLog([theString substringWithRange:[match rangeAtIndex:1]]);
说明:
正则表达式查找匹配的地方" firstName":然后是除了"之外的可变数量的符号(因为"显示数据开始的位置)。 ([^ \"] )标记正则表达式中的特定范围(以便您可以通过此行[theString substringWithRange:[match rangeAtIndex:1]]
单独提取它。[^ \"] 表示每个标志除了"(因为这是数据的结尾)。我知道这一开始可能会令人困惑。但是如果你花一些时间用它,你会发现它很容易。