将 JSON 反序列化为 C# 模型

Deserialize JSON to c# model

我收到一个 json 对象,例如

 {
    "designKey": 7470,
    "title": "Test1",
    "note": "Test1",
    "createdBy": "username",
    "creationDate": "2020-01-24T20:47:28.297"   },  
 {
    "designKey": 7472,
    "title": "Test2",
    "note": "Test2",
    "createdBy": "username",
    "creationDate": "2020-01-24T23:20:44.207"   },   
 {
    "designKey": 7473,
    "title": "Test3",
    "note": "Test3",
    "createdBy": "username",
    "creationDate": "2020-01-24T23:39:03.99"   }

所以我创建了模型来反序列化:

public class AssignDesignModel
    {
        public class DesignViewModel
        {
            public IList<DesignAssignViewModel> DesignAssignList { get; set; } = new List<DesignAssignViewModel>();
        }

        public class DesignAssignViewModel
        {
            public int DesignKey { get; set; }
            public string Title { get; set; }
            public string Note { get; set; }
            public string CreatedBy { get; set; }
            public DateTime CreationDate { get; set; }
        }
    }

然后我执行为:

 var rModel = JsonConvert.DeserializeObject<DesignViewModel>(response.content);

但是抛出异常:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Project.Models.AssignDesignModel+DesignViewModel' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.'

我搜索了类似的问题,但找不到解决方案。问候

我会删除 AssignDesignModelDesignViewModel,因为它们不是必需的。 JSON 文件(几乎)包含您定义为 class DesignAssignViewModel 的对象数组,所以我会这样调用反序列化:

型号:

public class DesignAssignViewModel
        {
            public int DesignKey { get; set; }
            public string Title { get; set; }
            public string Note { get; set; }
            public string CreatedBy { get; set; }
            public DateTime CreationDate { get; set; }
        }

反序列化:

var rModel = JsonConvert.DeserializeObject<DesignAssignViewModel[]>(response.content);

就是说,传入的 JSON 格式不正确,正如其他人指出的那样。因此,您可能应该首先处理该问题。

按照您现在定义 DesignViewModel 的方式,您的 JSON 必须如下所示:

{
    "DesignAssignList": [
        {
            "designKey": 7470,
            "title": "Test1",
            "note": "Test1",
            "createdBy": "username",
            "creationDate": "2020-01-24T20:47:28.297"
        },
        {
            "designKey": 7472,
            "title": "Test2",
            "note": "Test2",
            "createdBy": "username",
            "creationDate": "2020-01-24T23:20:44.207"
        },
        {
            "designKey": 7473,
            "title": "Test3",
            "note": "Test3",
            "createdBy": "username",
            "creationDate": "2020-01-24T23:39:03.99"
        }
    ]
}