Scott Feinberg
Scott Feinberg

Reputation: 574

Objective C Parsing JSON, Dictionary returns a NSCFString instead of NSArray

What I'm trying to do is take a JSON feed and then loop through the results. However I keep getting a string instead of an array when I get the object from the dictionary. Any ideas on what I'm doing wrong?

Here's the JSON:

[
  {
    "_id": "4f6d9a7c1d0b4900010007ee",
    "geo_triggers": [
      {
        "_id": "4fc3e5fdc7234e0001000002",
        "location": [1,1],
        "longitude": "1",
        "latitude": "1",
        "radius": 1,
        "location_name": "Test 1"
      },
      {
        "_id": "4fc61f3762f53f0001000043",
        "location": [-71.057673,42.355395],
        "longitude": "-71.057673",
        "latitude": "42.355395",
        "radius": 1000,
        "location_name": "Test2"
      }
    ]
  }
]

Here's the Objective C Code:

const char* className = class_getName([result class]);
NSLog(@"Result is a: %s", className);
NSLog(@"%@", result); //string
NSArray* json = [result objectForKey:@"result"]; //should be an array of dictionaries
NSLog(@"JSON Output: %@", json);
const char* className1 = class_getName([json class]);
NSLog(@"yourObject is a: %s", className1);

And here's the output:

Result is a: __NSDictionaryI
2012-10-10 17:15:15.165 App[12980:19d03] {
    result = "[{\"_id\":\"4f6d9a7c1d0b4900010007ee\",\"geo_triggers\":[{\"_id\":\"4fc3e5fdc7234e0001000002\",\"location\":[1.0,1.0],\"longitude\":\"1\",\"latitude\":\"1\",\"radius\":1,\"location_name\":\"Test 1\"},{\"_id\":\"4fc61f3762f53f0001000043\",\"location\":[-71.057673,42.355395],\"longitude\":\"-71.057673\",\"latitude\":\"42.355395\",\"radius\":1000,\"location_name\":\"Test2\"}]}]";
}
2012-10-10 17:15:15.166 App[12980:19d03] JSON Output: [{"_id":"4f6d9a7c1d0b4900010007ee","geo_triggers":[{"_id":"4fc3e5fdc7234e0001000002","location":[1.0,1.0],"longitude":"1","latitude":"1","radius":1,"location_name":"Test 1"},{"_id":"4fc61f3762f53f0001000043","location":[-71.057673,42.355395],"longitude":"-71.057673","latitude":"42.355395","radius":1000,"location_name":"Test2"}]}]
2012-10-10 17:15:15.166 App[12980:19d03] yourObject is a: __NSCFString
2012-10-10 17:15:15.166 App[12980:19d03] -[__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0xb331800

Upvotes: 2

Views: 2093

Answers (2)

S.P.
S.P.

Reputation: 3054

First need to decode the result. The above is JSON so I would suggest doing this

  1. If you don't already have it download JSONKit.h and include it in your project
  2. Then you can either do

    1. NSString* json = [result JSONString]; to see the output
      OR
    2. something like id jsonDict = [[JSONDecoder decoder] objectWithData:responseData]; After that you can do [jsonDict objectForKey:@"_id"]; ///etc

Upvotes: 1

rob mayoff
rob mayoff

Reputation: 385580

Your result variable points to a dictionary. The dictionary contains one key. That key is @"result". The value for that key is a string, @"[{\"_id\":\"4f6d9a7c1d0b4900010....

In other words, you haven't really deserialized your JSON. You need to take the value for key result and run it through a JSON deserializer.

Upvotes: 6

Related Questions