Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为这个json创建模型,c#_C#_Json - Fatal编程技术网

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

如何为这个json创建模型,c#,c#,json,C#,Json,如何为这个json创建模型 {“代码”:“1”,“消息”:“确定”,“响应”: {“用户”:{“id”:“124”,“用户类型”:“29”,“当前时间”:“2013-04-24T08:09:20.487+00:00”,“会话id”:“2bcf1c92-9b97-47d8-8995-b39cf7cbe2af”} 我写道: public class BaseResponse { public bool success; public String messag

如何为这个json创建模型

{“代码”:“1”,“消息”:“确定”,“响应”: {“用户”:{“id”:“124”,“用户类型”:“29”,“当前时间”:“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);
CreateTaskResponse Login=SimpleDataSerializer.ToObject(响应);
错误: Login.getUser().sessionid为null

这是生成的

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(json);
这就是生成的

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(json);

你使用解析器吗?考虑使用JSON.NET(我个人不是一个粉丝,但它似乎是JSON在.NET中的事实标准)谢谢,我会尝试的。我使用NethOnt.JSONARE你使用解析器?考虑使用JSON.NET(我个人不是风扇,但它似乎是JSON在.NET中的事实标准)谢谢,我会尝试。我使用Newtonsoft.Json