C#中的Json映射和序列化

4

我正在尝试在C#中将一个类似于这样的JSON字符串映射到一个对象。我的目的是将其发送到客户端(它是一个jQuery应用程序)。我想知道我是否做得对?

{"tasks":[
        {
            "id":-1,
            "name":"Gantt editor",
            "code":"",
            "level":0,
            "status":"STATUS_ACTIVE",
            "start":1372608000000,
            "duration":21,
            "end":1375113599999,
            "startIsMilestone":true,
            "endIsMilestone":false,
            "collapsed":false,
            "assigs":[]
        },
        {
            "id":"tmp_fk1372575559620",
            "name":"release",
            "code":"",
            "level":1,
            "status":"STATUS_ACTIVE",
            "start":1372608000000,
            "duration":1,
            "end":1372694399999,
            "startIsMilestone":false,
            "endIsMilestone":false,
            "collapsed":false,
            "assigs":[]
        }
        ],
"selectedRow":8,
"deletedTaskIds":[],
"resources":
    [
        {
        "id":"tmp_1",
        "name":"Resource 1"
        }
    ],
"roles":[
        {
            "id":"tmp_1",
            "name":"Project Manager"
        }
        ],
"canWrite":true,
"canWriteOnParent":true
}

这是我的映射方法:

public class Attributes
    {    
        public List<Task> _Task { get; set; }
        public List<Resource> _Resource { get; set; }
        public List<Role> _Role { get; set; }
        public bool _canWrite { get; set; }                      //"canWrite":true,
        public bool _canWriteOnParent { get; set; }              //"canWriteOnParent":true,
        public bool _selectedRow { get; set; }                   //"selectedRow":0,
        public string [] _DeletedTaskIds { get; set; }           //"deletedTaskIds":[],
    }

    public class Task
    {
        private string _id { get; set; }
        private string _name { get; set; }
        private string _code { get; set; }
        private int _level { get; set; }
        private string _status { get; set; }
        private int _start { get; set; }              //“start”:1348696800000,
        private int _duration { get; set; }           //“duration”:10,
        private int _end { get; set; }                //“end”:1349906399999,
        private bool _startIsMilestone { get; set; }  //“startIsMilestone”:false,
        private bool _endIsMilestone { get; set; }    //“endIsMilestone”:false,
        public List<Assign> _assigns { get; set; }    //“assigs”:[…],
        private string _depends { get; set; }         //“depends”:”7:3,8″,
        private string _description { get; set; }     //“description”:”Approval of testing”,
        private int _progress { get; set; }           //“progress”:20
    }

    public class Assign 
    {
        private string _resourceId { get; set; }      //“resourceId”:”tmp_1″,
        private string _id { get; set; }              //“id”:”tmp_1345560373990″,
        private string _roleId { get; set; }          //“roleId”:”tmp_1″,
        private string _effort { get; set; }          //“effort”:36000000

    }

    public class Resource
    {
        private string _id { get; set; }              //“id”:”tmp_1″,
        private string _name { get; set; }            //“name”:”Resource 1″
    }

    public class Role
    {
        private string _id { get; set; }              //“id”:”tmp_1″,
        private string _name { get; set; }            //“name”:”Project Manager”
    }

如果我做对了,那么如何将它序列化为一个对象并发送到客户端呢?
2个回答

8

这里有一个极好的工具,可以将Json映射到C#,以下是您的映射示例:

public class Task
{
    public object id { get; set; }
    public string name { get; set; }
    public string code { get; set; }
    public int level { get; set; }
    public string status { get; set; }
    public object start { get; set; }
    public int duration { get; set; }
    public object end { get; set; }
    public bool startIsMilestone { get; set; }
    public bool endIsMilestone { get; set; }
    public bool collapsed { get; set; }
    public List<object> assigs { get; set; }
}

public class Resource
{
    public string id { get; set; }
    public string name { get; set; }
}

public class Role
{
    public string id { get; set; }
    public string name { get; set; }
}

public class RootObject
{
    public List<Task> tasks { get; set; }
    public int selectedRow { get; set; }
    public List<object> deletedTaskIds { get; set; }
    public List<Resource> resources { get; set; }
    public List<Role> roles { get; set; }
    public bool canWrite { get; set; }
    public bool canWriteOnParent { get; set; }
}

还有另一种将json映射到C#的简洁方法(我相信它随Visual Studio 2012 -> Web Essentials一起提供),您可以转到编辑-> 特殊粘贴-> 将JSON作为类粘贴

编辑

由于您使用Json.NET(您可以从NuGet获取它),您可以像这样反序列化您的json:

RootObject deserialized = JsonConvert.DeserializeObject<RootObject>(data);

嗨,我需要使用你帮助我创建的结构来创建上述JSON字符串的对象。最好的方法是什么? - Poorya

1
你需要更改类和属性的格式。这是正确的格式。 试一下。
public class Task
{
    public object id { get; set; }
    public string name { get; set; }
    public string code { get; set; }
    public int level { get; set; }
    public string status { get; set; }
    public object start { get; set; }
    public int duration { get; set; }
    public object end { get; set; }
    public bool startIsMilestone { get; set; }
    public bool endIsMilestone { get; set; }
    public bool collapsed { get; set; }
    public List<object> assigs { get; set; }
}

public class Resource
{
    public string id { get; set; }
    public string name { get; set; }
}

public class Role
{
    public string id { get; set; }
    public string name { get; set; }
}

public class RootObject
{
    public List<Task> tasks { get; set; }
    public int selectedRow { get; set; }
    public List<object> deletedTaskIds { get; set; }
    public List<Resource> resources { get; set; }
    public List<Role> roles { get; set; }
    public bool canWrite { get; set; }
    public bool canWriteOnParent { get; set; }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接