2013-06-21 43 views
-2

我从API获取一些数据,数据是一个字符串。作为字符串的JSON?我怎样才能访问它?

po JSON 
$22 = 0x26305840 { 
    "thing_id" = 5192f9053000001; 
    status = "{\"thing_request_id\":\"51c3a0608906f101f\",\"thing_id\":\"5192f9053000001\",\"status\":\"PICKUP\"}"; 
} 

如何访问内部状态键?不是“stat =”之一。

我通过socketIO连接访问数据。

+1

您想使用的库将其反序列化到本地的对象。这个问题可能会给你正确的方向http://stackoverflow.com/questions/7172001/serialize-and-deserialize-objective-c-objects-into-json – evanmcdonnal

+0

是的,除了一些非常有限的情况下,你可以处理使用正则表达式,您需要将JSON解析为目标语言中的对象,然后才能访问JSON> –

+0

中的值。帮助不大。 – jdog

回答

0

此代码适用于使用未加引号的键修复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; 
} 
0

我建议增加JSONKit这将使它简单的把它放在字典和访问每个元素得到了它。

NSData *jsonData = [yourJSONString dataUsingEncoding:NSUTF8String]; 
NSDictionary *dictionary = [jsonData objectFromJSONData]; 

然后你可以像访问普通的NSDictionary一样访问每个元素。

[dictionary objectForKey:@"status"];//etc 

你可以得到JSONKit here

1

你所寻找的是NSJSONSerialization。

你可以用它喜欢:

NSData *data = [stringJSON dataUsingEncoding:NSUTF8StringEncoding]; 
NSError *error; 
id *yourJSON = [JSONObjectWithData:webData options:0 error:&error]; 

if(error) { 
    NSLog(@"Error: %@", error); 
} else { 
    NSLog(@"You JSON: %@", yourJSON); 
} 

观察到,它可以返回数组或字典是很重要的,这个代码仅是一个例子。

文档:

http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html

0

效率不高的代码,但我只是简单地调用它两次,它的工作原理。

NSError *jsonParsingError = nil; 

NSDictionary *JSON = 
[NSJSONSerialization JSONObjectWithData: [packet.args[0] dataUsingEncoding:NSUTF8StringEncoding] 
           options: NSJSONReadingMutableContainers 
            error: &jsonParsingError]; 

NSString *lastStatus = [JSON objectForKey:@"status"]; 
    NSDictionary *lastStatusDict = [JSON objectForKey:@"status"]; 


    NSError *jsonParsingError = nil; 
    NSDictionary *INNERJSON = 
    [NSJSONSerialization JSONObjectWithData: [lastStatus dataUsingEncoding:NSUTF8StringEncoding] 
            options: NSJSONReadingMutableContainers 
             error: &jsonParsingError]; 
NSString *innerStatus = [INNERJSON objectForKey:@"status"]; 

然后我就可以用它

if ([innerStatus isEqualToString:kSENT]) { ..... 
相关问题