简体   繁体   中英

Converting JSON to Objective-C object using key value coding

I am an Objective-C newbie coming from the Java world, so I am trying to find out how to do something that would be (relatively) straightforward to do in Java.

In an iPhone app I am accessing a rest webservice that returns JSON. I am using the AFNetworking framework, which provides support for using the NSJSONSerialization class added to iOS 5. I would like to know if there is an "automatic" way to turn the JSON object that is returned into one of my "strongly" typed classes (for example, my UserAccount class).

In other words, I'm trying to do something like:

id json = ... call webservice that returns json, which AFNetworking parses ...
UserAccount myUserAccount = [UserAccount alloc] init];
//then here I'd like something that iterates through all the properties on the 
//json object and sets them on the myUserAccount object using key value coding

Is there a straightforward way to accomplish this?

Nope, not that I know of. The most common way is to add a constructor to your class called initWithDictionary , that accepts the JSON dictionary and gets the relevant information from it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM