c#json将字符串反序列化成对象时不新建类的做法

在服务端代码文件中加上struct结构体就能解决

struct LocationInfo
    {
        public string LocationID { get; set; }
        public string ServerLicenseID { get; set; }
        public string OrganizationID { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }
    }

这样就直接可以反序列化成LocationInfo了

并且是可以结构体里面嵌套结构体的

    struct Tag
    {
        public string log_id { get; set; }
        public string result_num { get; set; }
        public List<Result> result { get; set; }
    }
    struct Result
    {
        public string score { get; set; }
        public string root { get; set; }
        public string keyword { get; set; }
    }

这是嵌套的

        Tag tag = JsonConvert.DeserializeObject<Tag>(resultstr);
        foreach (Result item in tag.result)
        {
            
        }    

可以直接使用

原文地址:https://www.cnblogs.com/heyiping/p/11798611.html