无法将当前JSON对象(例如{" name":" value"})反序列化为类型System.Collections.Generic.List`1'

时间:2017-07-12 10:19:32

标签: c# json

我有以下JSON

{
"d" : [
{
"__metadata": {
"uri": "http://tools/sites/xxxen/HSE_registreringer/_vti_bin/listdata.svc/ddeer(95)", "etag": "W/\"2\"", "type": "Microsoft.SharePoint.DataService.ddeerItem", "edit_media": "http://tools/sites/xxxen/HSE_registreringer/_vti_bin/listdata.svc/ddeer(95)/$value", "media_src": "http://tools/sites/xxxen/HSE_registreringer/ddeer/ESV Indfasning af ddeer/ESV_MONO A-625 Anion Exchange Resin.docx", "content_type": "application/octetstream", "media_etag": "\"{A5D87498-016D-4FB2-A674-53CC6BB15B6F},3\""
}, "Title": "ESV_MONO A-625", "Id": 95, "Path": "/sites/xxxen/HSE_registreringer/ddeer/ESV Indfasning af ddeer"
}, {
"__metadata": {
"uri": "http://tools/sites/xxxen/HSE_registreringer/_vti_bin/listdata.svc/ddeer(96)", "etag": "W/\"5\"", "type": "Microsoft.SharePoint.DataService.ddeerItem", "edit_media": "http://tools/sites/xxxen/HSE_registreringer/_vti_bin/listdata.svc/ddeer(96)/$value", "media_src": "http://tools/sites/xxxen/HSE_registreringer/ddeer/ESV Indfasning af ddeer/ESV_Indfasning af AMBERJET 1500 H.docx", "content_type": "application/octetstream", "media_etag": "\"{DC1B3362-C845-40B3-97F3-A76BF2C2298F},9\""
}, "Title": "Indfasning af 1500 H", "Id": 96, "Path": "/sites/xxxen/HSE_registreringer/ddeer/ESV Indfasning af ddeer"
}, {
"__metadata": {
"uri": "http://tools/sites/xxxen/HSE_registreringer/_vti_bin/listdata.svc/ddeer(104)", "etag": "W/\"5\"", "type": "Microsoft.SharePoint.DataService.ddeerItem", "edit_media": "http://tools/sites/xxxen/HSE_registreringer/_vti_bin/listdata.svc/ddeer(104)/$value", "media_src": "http://tools/sites/xxxen/HSE_registreringer/ddeer/SSV-indfasning af Castrol Radicool.docx", "content_type": "application/octetstream", "media_etag": "\"{A0F7213E-8F53-4023-8BE0-B721A4238D32},5\""
}, "Title": "indfasning radicool", "Id": 104, "Path": "/sites/xxxen/HSE_registreringer/ddeer"
}, {
"__metadata": {
"uri": "http://tools/sites/xxxen/HSE_registreringer/_vti_bin/listdata.svc/ddeer(105)", "etag": "W/\"2\"", "type": "Microsoft.SharePoint.DataService.ddeerItem", "edit_media": "http://tools/sites/xxxen/HSE_registreringer/_vti_bin/listdata.svc/ddeer(105)/$value", "media_src": "http://tools/sites/xxxen/HSE_registreringer/ddeer/AVV_Udfasning af Araldit.docx", "content_type": "application/octetstream", "media_etag": "\"{7BB35C40-5AA8-4D8D-A1B1-D76E210B54B1},2\""
}, "Title": "Araldit ARC ", "Id": 105, "Path": "/sites/xxxen/HSE_registreringer/ddeer"
}, {
"__metadata": {
"uri": "http://tools/sites/xxxen/HSE_registreringer/_vti_bin/listdata.svc/ddeer(106)", "etag": "W/\"2\"", "type": "Microsoft.SharePoint.DataService.ddeerItem", "edit_media": "http://tools/sites/xxxen/HSE_registreringer/_vti_bin/listdata.svc/ddeer(106)/$value", "media_src": "http://tools/sites/xxxen/HSE_registreringer/ddeer/AVV_Udfasning af Casco Strong Rapid.docx", "content_type": "application/octetstream", "media_etag": "\"{45DB0319-F298-4776-B69F-7F3260C2318A},2\""
}, "Title": "AVV af Casco Strong Epoxy", "Id": 106, "Path": "/sites/xxxen/HSE_registreringer/ddeer"
}, {
"__metadata": {
"uri": "http://tools/sites/xxxen/HSE_registreringer/_vti_bin/listdata.svc/ddeer(107)", "etag": "W/\"2\"", "type": "Microsoft.SharePoint.DataService.ddeerItem", "edit_media": "http://tools/sites/xxxen/HSE_registreringer/_vti_bin/listdata.svc/ddeer(107)/$value", "media_src": "http://tools/sites/xxxen/HSE_registreringer/ddeer/HCV_Udfasning af Belzona 1211.docx", "content_type": "application/octetstream", "media_etag": "\"{BC4F04EE-3D48-44FF-B4D8-909023177E0D},2\""
}, "Title": "HCV af Belzona 1211", "Id": 107, "Path": "/sites/xxxen/HSE_registreringer/ddeer"
}, {
"__metadata": {
"uri": "http://tools/sites/xxxen/HSE_registreringer/_vti_bin/listdata.svc/ddeer(108)", "etag": "W/\"3\"", "type": "Microsoft.SharePoint.DataService.ddeerItem", "edit_media": "http://tools/sites/xxxen/HSE_registreringer/_vti_bin/listdata.svc/ddeer(108)/$value", "media_src": "http://tools/sites/xxxen/HSE_registreringer/ddeer/AVV_Udfasning af Araldit 02.docx", "content_type": "application/octetstream", "media_etag": "\"{127F69BC-131A-4C92-8436-AF3D3EF84C54},3\""
}, "Title": "Araldit 858", "Id": 108, "Path": "/sites/xxxen/HSE_registreringer/ddeer"
}, {
"__metadata": {
"uri": "http://tools/sites/xxxen/HSE_registreringer/_vti_bin/listdata.svc/ddeer(109)", "etag": "W/\"6\"", "type": "Microsoft.SharePoint.DataService.ddeerItem", "edit_media": "http://tools/sites/xxxen/HSE_registreringer/_vti_bin/listdata.svc/ddeer(109)/$value", "media_src": "http://tools/sites/xxxen/HSE_registreringer/ddeer/AVV_Indfasning af KEMA TL-676 Trykluft.docx", "content_type": "application/octetstream", "media_etag": "\"{14F342E7-D7F1-45E0-AF25-CFCBC0845A13},6\""
}, "Title": "Trykluft", "Id": 109, "Path": "/sites/xxxen/HSE_registreringer/ddeer"
}, {
"__metadata": {
"uri": "http://tools/sites/xxxen/HSE_registreringer/_vti_bin/listdata.svc/ddeer(110)", "etag": "W/\"2\"", "type": "Microsoft.SharePoint.DataService.ddeerItem", "edit_media": "http://tools/sites/xxxen/HSE_registreringer/_vti_bin/listdata.svc/ddeer(110)/$value", "media_src": "http://tools/sites/xxxen/HSE_registreringer/ddeer/AVV_Udfasning af ITW trykluft.docx", "content_type": "application/octetstream", "media_etag": "\"{893A9D28-EB70-4F1C-BF87-3AE145AF1B15},2\""
}, "Title": "Trykluft", "Id": 110, "Path": "/sites/xxxen/HSE_registreringer/ddeer"
}, {
"__metadata": {
"uri": "http://tools/sites/xxxen/HSE_registreringer/_vti_bin/listdata.svc/ddeer(111)", "etag": "W/\"1\"", "type": "Microsoft.SharePoint.DataService.ddeerItem", "edit_media": "http://tools/sites/xxxen/HSE_registreringer/_vti_bin/listdata.svc/ddeer(111)/$value", "media_src": "http://tools/sites/xxxen/HSE_registreringer/ddeer/Marloherm.docx", "content_type": "application/octetstream", "media_etag": "\"{2BA74EE4-1F57-48B5-8BE5-CCB27C0B0E02},1\""
}, "Title": "Marloherm", "Id": 111, "Path": "/sites/xxxen/HSE_registreringer/ddeer"
}
]
}

我尝试使用以下语法对其进行反序列化

SPDocument list = JsonConvert.DeserializeObject<SPDocument>(response.Content);

但继续收到上述错误&#39;

我的模型类如下:

  public class SPResult
{
    public string Name { get; set; }
    public string Title { get; set; }
    public string Path { get; set; }
    public List<Metadata> __metadata { get; set; }
}

public class SPObject
{
    public List<SPResult> results { get; set; }
}

public class SPDocument
{
    public List<SPObject> d { get; set; }
}
public class Metadata
{
    public string uri { get; set; }
    public string etag { get; set; }
    public string type { get; set; }

}

查看同一问题的大多数其他问题,但无法弄清楚如何处理此问题。

错误发生在路径&#39;

2 个答案:

答案 0 :(得分:2)

在网站json2csharp.com上,您可以为json生成类。 在你的情况下,这可以是:

public class Metadata
{
    public string uri { get; set; }
    public string etag { get; set; }
    public string type { get; set; }
    public string edit_media { get; set; }
    public string media_src { get; set; }
    public string content_type { get; set; }
    public string media_etag { get; set; }
}

public class D
{
    public Metadata __metadata { get; set; }
    public string Title { get; set; }
    public int Id { get; set; }
    public string Path { get; set; }
}

public class RootObject
{
    public List<D> d { get; set; }
}

因此在您的代码中SPObject是不必要的。

SPDocument应该是:

public class SPDocument
{
    public List<SPResult> d { get; set; }
}

答案 1 :(得分:1)

根据JSON数据,您的SPObject不包含SPResult

SPObject是您的SPResult

删除它,然后就完成了。

您的SPDocument应该是:

public class SPDocument
{
    public List<SPResult> d { get; set; }
}