开发者

parsing json feeds into iphone

i was trying to parse public json flickr feed into iphone.i m getting an error..

-JSONValue failed. Error trace is: (
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Unrecognised leading character\"开发者_如何学运维 UserInfo=0x4b43100 {NSLocalizedDescription=Unrecognised leading character}"
).....

below is the json feed:

http://api.flickr.com/services/feeds/photos_public.gne?tags=hackdayindia&lang=en-us&format=json.....

i d be so greatful if you guys could help me out

parsing json feeds into iphone


Your response data is not valid JSON because of the jsonFlickrFeed() callback returned by the Flickr API. In order to obtain valid JSON, add nojsoncallback=1 to your query. Using your example, the corresponding URL is:

http://api.flickr.com/services/feeds/photos_public.gne?tags=hackdayindia&lang=en-us&format=json&nojsoncallback=1

If you use the response from the URL above in http://jsonlint.com, it’ll tell you it’s valid JSON.

nojsoncallback is documented in http://www.flickr.com/services/api/response.json.html


A quick example of how to read that response. The top level element of the JSON string is an object, which is usually mapped to a dictionary by JSON parsers:

// Obtain the JSON string from Flickr API
NSString *jsonString = …;

// Parse the JSON string into a dictionary
// (in this example, via SBJSON)
NSDictionary *responseObject = [jsonString JSONValue];

// The dictionary has an entry called "items", which is an array
NSArray *items = [responseObject objectForKey:@"items"];

// Iterate over the items. Each item is an object, hence a dictionary
for (NSDictionary *item in items) {
    // Each item dictionary has an entry called "author_id", which is a string
    NSString *authorId = [item objectForKey:@"author_id"];

    // Log the author id
    NSLog(@"author_id = %@", authorId);
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜