开发者

parse json feeds using jsonkit iOS [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

开发者_运维知识库

Closed 5 years ago.

Improve this question

I'm trying to use the JSONKIt found here https://github.com/johnezang/JSONKit to parse through a JSON Feed and put into objective-c objects. I'm new at iOS and don't really know where to start. Are there any good tutorials for using this library?


After googling, I didn't find any tutorials but using JSONKit should be self explanatory.

After downloading your JSON feed using NSURLConnection or ASIHTTPRequest simply create a dictionary of all the objects in the JSON feed like so:

//jsonString is your downloaded string JSON Feed
NSDictionary *deserializedData = [jsonString objectFromJSONString];

//Helpful snippet to log all the deserialized objects and their keys
NSLog(@"%@", [deserializedData description]);

After creating a dictionary you can simply do something like this:

    NSString *string = [deserializedData objectForKey:@"someJSONKey"];

And that is the basics behind JSONKit.

JSONKit is much more powerful of course, you can find some of the other things you can do with it in JSONKit.h


I would becareful about making the assumption that objectFromJSONString is returns an NSDictionary, it can very well return an array, or nil, especially if the server returns some rarely used and thought of error.

A more appropriate action would be:

NSError *error;
id rawData = [jsonString objectFromJSONStringWithParseOptions:JKParseOptionNone error:&error];

if ( error != nil ) {
    // evaluate the error and handle appropriately
}

if ( [rawData isKindOfClass:[NSDictionary class]] ) {
    // process dictionary
}
else if ( [rawData isKindOfClass:[NSArray class]] ) {
    // process array
}
else {
    // someting else happened, 'rawData' is likely 'nil'
    // handle appropriately
}

Without these checks, you could very well end up with a runtime error because the server returned someting unexpected.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜