zl程序教程

您现在的位置是:首页 >  前端

当前栏目

把Json字符串转化为对应的类

JSONJSON 字符串 对应 转化
2023-09-11 14:19:20 时间

1、json字符串形式如下:

 var data= "{\"myList\":[{\"name\":\"上海市中油\",\"code\":\"shanghaishizhyou\",\"rgName\":\"中油\",\"type\":\"sb\",\"date\":\"2021-07-01\"},{\"name\":\"上海市石化\",\"code\":\"shanghaishiwuxian\",\"rgName\":\"中油\",\"type\":\"sb\",\"date\":\"2021-07-01\"},{\"name\":\"上海中油\",\"code\":\"shanghaishebao\",\"rgName\":\"中油\",\"type\":\"sb\",\"date\":\"2021-07-01\"}]}";

2、设置对应的类:

 public class myclass
    {
       public List<MyList> MyList { get; set; }
    }
    public class MyList
    {
        public string name { get; set; }
        public string code { get; set; }
        public string rgName { get; set; }
        public string type { get; set; }
        public string date { get; set; }
    }

3、转化方法:

var aaa = JsonConvert.DeserializeObject<myclass>(data);