如何为这个json创建模型,c#

时间:2013-04-24 08:36:54

标签: c# json

如何为这个json创建模型?

  

{“code”:“1”,“message”:“OK”,“回复”:   { “用户”:{ “ID”: “124”, “用户类型”: “29”, “CURRENTTIME”: “2013-04-24T08:09:20.487 + 00:00”, “会话ID”:“2bcf1c92-9b97 -47d8-8995-b39cf7cbe2af“}}}

我写道:

public class BaseResponse
    {
        public bool success;
        public String message;
    }

    public class CreateTaskResponse : BaseResponse
    {
        CreateTaskResponseData data = new CreateTaskResponseData();

        public User getUser()
        {
            return data.getUser();
        }
    }

    class CreateTaskResponseData
    {
        User user = new User();
        public User getUser()
        {
            return user;
        }
    }

public class User
{
    public int id;
    public int usertype;
    public string currenttime;
    public string sessionid;
}

并解析:

CreateTaskResponse Login = SimpleDataSerializer.ToObject<CreateTaskResponse>(response);

错误: Login.getUser()。sessionid为null

1 个答案:

答案 0 :(得分:3)

这是this site生成的

public class User
{
    public string id { get; set; }
    public string usertype { get; set; }
    public string currenttime { get; set; }
    public string sessionid { get; set; }
}

public class Response
{
    public User user { get; set; }
}

public class RootObject
{
    public string code { get; set; }
    public string message { get; set; }
    public Response response { get; set; }
}

var obj = JsonConvert.DeserializeObject<RootObject>(json);