1. 程式人生 > >C#中將json轉化成list

C#中將json轉化成list

在程式集中新增引用 System.Web.Extensions

在類中新增引用

using System.Runtime.Serialization;
using System.Web.Script.Serialization;

{"Total":"0",
"Rows":
[
{
"id":"31",
"project":"6",
"project_name":"測試",
"name":"哈哈!",
"assignedTo":"liming",
"realname":"李明",
"estStarted":"2016/11/23",
"realStarted":"2000/01/01",
"status":"wait"
},
{
"id":"32",
"project":"6",
"project_name":"嘻嘻",
"name":"你好",
"assignedTo":"wangwu",
"realname":"王五",
"estStarted":"2016/11/23",
"realStarted":"2016/11/23",
"status":"wait"
}
]}

int IndexofA = jsonText.IndexOf("[");
int IndexofB = jsonText.IndexOf("]");
string Ru = jsonText.Substring(IndexofA , IndexofB - IndexofA+1);

JavaScriptSerializer Serializer = new JavaScriptSerializer();
List<Test> objs = Serializer.Deserialize<List<Test>>(Ru);

//實體類

 public class Test
        {
            public int id { get; set; }
            public string project { get; set; }
            public string project_name { get; set; }
            public string name { get; set; }
            public string assignedTo { get; set; }
            public string realname{ get; set; }
            public string estStarted { get; set; }
            public string realStarted { get; set; }
            public string status { get; set; }
        }