-2

I am getting back some data from an API and the data is a string.

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

How can I access the inner status key? NOT the "stat =" one.

I am getting access to the data via a socketIO connection.

Undo
  • 25,519
  • 37
  • 106
  • 129
jdog
  • 10,351
  • 29
  • 90
  • 165
  • 1
    You want to use a library to deserialize it into native objects. This question may send you in the right direction http://stackoverflow.com/questions/7172001/serialize-and-deserialize-objective-c-objects-into-json – evanmcdonnal Jun 21 '13 at 00:56
  • Yep, except for some very limited cases which you can maybe handle with regular expressions, you need to parse JSON into objects in your target language before accessing values in the JSON> – Hot Licks Jun 21 '13 at 00:59
  • @HotLicks ummmm what? – jdog Jun 21 '13 at 01:12
  • Parse. You know, with NSJSONSerialization, JSON-Framework, JSONKit, yajl-objc, TouchJSON, or ObjFW. Take your pick. – Hot Licks Jun 21 '13 at 01:56

4 Answers4

1

What you are looking for is NSJSONSerialization.

you can use it like:

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);
}

It is important to observe that it can return an array or a dictionary, this code is only an example.

Documentation:

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

ggrana
  • 2,335
  • 2
  • 21
  • 31
0

This code work well for fixing JSON with unquoted key. I got it from 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;
}
novalagung
  • 10,905
  • 4
  • 58
  • 82
0

I would suggest adding JSONKit which will make it simple to put it in a dictionary and access each element.

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

Then you can access each element the same as you would a normal NSDictionary.

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

You can get JSONKit here

Ben Avery
  • 1,724
  • 1
  • 20
  • 33
0

Not efficient code but I just simply call it twice and it works.

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"];

THEN I CAN USE IT

if ([innerStatus isEqualToString:kSENT]) { .....
jdog
  • 10,351
  • 29
  • 90
  • 165