开发者

NSDictionaries and NSArrays from JSON from complicated YouTube API

开发者 https://www.devze.com 2023-01-29 10:37 出处:网络
I\'m making an awesome iPhone app which searches for YouTube videos using the JSON API. However, Google is lazy so they just transformed the ATOM feed into JSON. Things look like this:

I'm making an awesome iPhone app which searches for YouTube videos using the JSON API. However, Google is lazy so they just transformed the ATOM feed into JSON. Things look like this:

feed->entry[0]->author[0]->name->$t

This means that getting the information out of the NSArray is difficult, as I need to get a value of a key of an object of an array of an object of an array of an object of a key.

To check if the structure is correct, I can choose two things:

  1. Use a huge amount of code for each item I want to check if the JSON was correct.
  2. Wrap everything in a @try block.

I'd like to choose the second one. The problem is that some time ago I read that this is bad practice. Is it? And if so, 开发者_开发百科is there a shorter way to validate the NSArrays en NSDictionaries? My app may never crash, not even if the user remover the processor at runtime, so not checking at all is not an option.

Can you please help me? Thanks.


Have you tried the GData API? I'm using it for my application ( http://itunes.apple.com/us/app/skystop/id392782307?mt=8 ) for the Youtube Feed. It basically spits out an XML file for whatever you've requested and you can convert it right into a plist file or an NSArray.


i'm not sure i understand, the API itself works in JSON instead of ATOM so you need to di into every item ? If this is so then you are right, not much you can do except to seardch the web for helper libraries that might have been made even in google code to support this API.

In any case #2 is bad practice first of all since try catch usually consume more system resources then simple boolean cheek or checks. Second once you are in the catch block you are kind of in problem since all you can do is print an error to the user or yourself, if you want to go on parsing and checking, you can't... and last but not least (I'm sure there are reasons I'm not thinking of) except for the message you might get with the exception u are never to sure where it came from...


Are you parsing the JSON yourself? If so, I suggest using an external framework to do the work for you. I use Json-framework in a few of my own projects and it does the job just fine.

http://code.google.com/p/json-framework/

0

精彩评论

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