0

I receive json from a webservice into a NSMutableData.

That gets converted into a NSDictionary using TouchJson.

    NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:responseData error:&error];
NSString *strData = [dictionary objectForKey:@"cars"];

I then retrieve a string from a key from that dictionary.

The string looks like below

     {
        b = "http://schemas.datacontract.org/";
        car =     (
                    {
                "car_name" = "Honda Civic";
                year = 2011;
                "dealer" = "local honda dealer";
                "bought on" = {
                    nil = 1;
                };
                "license_number" = 1234567;
                status = ReadyToGo;
            }
)};

Essentially there can be 'n' records against the 'car' key.

when I try to convert the above to NSData using

NSData *jsonData = [strData dataUsingEncoding:NSUTF8StringEncoding];

and also

NSData *jsonData = [strData dataUsingEncoding:[NSString defaultCStringEncoding]];

but I get

[__NSCFDictionary dataUsingEncoding:]: unrecognized selector sent to instance 0x532bb70

I have tried a few other encodings available and xcode still threw up.

How can I figure out the encoding being used?

This is my first attempt at deserealizing json in objective-c.

What am I missing/doing wrong here?

Thanks

CF_Maintainer
  • 973
  • 2
  • 12
  • 30

1 Answers1

1

I think it's not a string at all....

change to this and test....

NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:responseData error:&error];
NSDictionary *carsDictionary = [dictionary objectForKey:@"cars"];
NSArray *arrayOfCarDictionaries = [carsDictionary objectForKey:@"car"];
NWCoder
  • 5,296
  • 1
  • 26
  • 23
  • I will give that a try. I was later going to convert the NSData to another dictionary and pull out the value for the key 'car' and then deserialize it using touchjson. Is this the wrong way to go about it? – CF_Maintainer Jun 08 '11 at 16:03
  • Yeah, the json deserializer works on the objects and the subobjects, so they are already properly decoded. All objects and subobjects should be dictionaries, arrays, or key/value pairs. – NWCoder Jun 08 '11 at 16:04
  • That worked!!!. Thanks. Firstly, I thought I would have to feed it specific pieces to deserialize. Secondly what tripped me is that the following worked : NSString *strData = [dictionary objectForKey:@"cars"]; NSLog(@"cars : %@", strData); which gave me an impression that I receiving a string for that key. – CF_Maintainer Jun 08 '11 at 16:10