开发者

couldn't serialize an NSObject, in Json

开发者 https://www.devze.com 2022-12-28 03:28 出处:网络
I\'m writing an iPhone app using JSON fr开发者_StackOverflowamework. I couldn\'t serialize an NSObject, I found only serialize for NSString, NSArray, NSData, but I want to serialize a generic object f

I'm writing an iPhone app using JSON fr开发者_StackOverflowamework. 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 Objective-C?

Do you know any solution?


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.


You might want to checkout my implementation for serializing NSObjects to JSON and vice versa - https://github.com/QBurst/KVCObjectSerializer

0

精彩评论

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