views:

156

answers:

1

Hello I'm writing an iPhone app using JSON framework. I couldn't serialize an NSObject, I found only serialize for nsstring, nsarray, nsdata, but I want to serialize a generic object from template object; any class library for this?

I have a user class, with userid, username and password. I want to serialize this class and send to server. After I want to deserialize result from server. Result could be class User, or any other class. In c# I have this: (In this case I use JSONRpc)

private int userid; [JsonIgnore] public int Userid { get { return userid; } set { userid = value; } } private string username; [JsonProperty("username")] public string Username { get { return username; } set { username = value; } } private string password; [JsonProperty("password")] public string Password { get { return password; } set { password = value; } } And method for response is: public JsonRpcResponse Invoke(string method, params object[] args). (T = template, in this case User) I can do this in Objectiv C?

Do you know any solution?

+1  A: 

No you can't. You need to turn the object into a straight NSDictionary or NSArray first. Write a method called jsonRepresentation and then one initFromJSONRepresentation. Not very hard in most cases.

Felixyz
Thanks, it's working with NSDictionary and NSArray.
ovidiu