此代码适用于使用未加引号的键修复JSON。我从https://coderwall.com/p/tdra3w
- (NSString *)fixJSON:(NSString *)s {
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"[{,]\\s*(\\w+)\\s*:"
options:0
error:NULL];
NSMutableString *b = [NSMutableString stringWithCapacity:([s length] * 1.1)];
__block NSUInteger offset = 0;
[regexp enumerateMatchesInString:s
options:0
range:NSMakeRange(0, [s length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSRange r = [result rangeAtIndex:1];
[b appendString:[s substringWithRange:NSMakeRange(offset, r.location - offset)]];
[b appendString:@"\""];
[b appendString:[s substringWithRange:r]];
[b appendString:@"\""];
offset = r.location + r.length;
}];
[b appendString:[s substringWithRange:NSMakeRange(offset, [s length] - offset)]];
return b;
}
来源
2013-06-21 00:59:47
nvl
您想使用的库将其反序列化到本地的对象。这个问题可能会给你正确的方向http://stackoverflow.com/questions/7172001/serialize-and-deserialize-objective-c-objects-into-json – evanmcdonnal
是的,除了一些非常有限的情况下,你可以处理使用正则表达式,您需要将JSON解析为目标语言中的对象,然后才能访问JSON> –
中的值。帮助不大。 – jdog