1

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?

2
  • 1
    No, you need to reconstruct them yourself. Commented Nov 9, 2012 at 3:29
  • I agree with the below answer. You can also check stackoverflow.com/questions/7284391/… Commented Nov 9, 2012 at 3:52

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this info was helpful. I figured out how to somewhat genericize this by iterating over the keys in the NSDictionary and using that to set property values on my object using key value coding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.