开发者

JSonKit deserialization problem

I have JSON as NSString:

[{"bus_number":"1","created_at":"2011-08-15T23:07:52Z","id":1,"model":"Setra","registar_number":"123456","seats":50,"tour_id":1,"updated_at":"2011-08-15T23:07:52Z"},{"bus_number":"2","created_at":"2011-08-15T23:07:52Z","id":2,"model":"Mercedes","registar_number":"2234","seats":60,"tour_id":1,"updated_at":"2011-08-15T23:07:52Z"}]

and I'm trying to convert this NSString to NSDictionary.

When I use:

NSDictionary *sourceDictionary = [[response bodyAsString] objectFromJSONString];

I get NSDictionary like this:

(
    {
    "bus_number" = 1;
    "created_at" = "2011-08-15T23:07:52Z";
    id = 1;
    model = Setra;
    "registar_number" = 123456;
 开发者_开发知识库   seats = 50;
    "tour_id" = 1;
    "updated_at" = "2011-08-15T23:07:52Z";
},
    {
    "bus_number" = 2;
    "created_at" = "2011-08-15T23:07:52Z";
    id = 2;
    model = Mercedes;
    "registar_number" = 2234;
    seats = 60;
    "tour_id" = 1;
    "updated_at" = "2011-08-15T23:07:52Z";
}

)

but, there is a problem with this NSDictionary, I can't access to their elements, some elements aren't NSStrings (like model).

How to convert JSONString to NSDictionary with NSStrings?

EDIT:

When I try to Log in console this with:

for (id key in sourceDictionary) {
            NSLog(@"key: %@, value: %@", key, [sourceDictionary objectForKey:key]);
        }

I get exception:

-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x1c3a50


If you're getting this from RestKit, and it looks like you are, you can just do this:

NSMutableDictionary *myDictionary = [response.body mutableObjectFromJSONData];

This way, you're pulling in the raw the binary data and you can avoid some unnecessary (and costly) conversions back and forth to strings in the process. Also, I stipulated mutable, because chances are you'll want to manipulate or muck about with the data when you get it ... again, there are costs involved in conversions between non-mutable and mutable objects from the JSONKit superclasses ... so by going with mutable from the outset, you just saved yourself some time.


Those are actually NSStrings, they just don't have quotation marks when you print them out to the console.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜