2012-04-12 147 views
2

我正在使用json.net來執行在IIS託管的寧靜服務中處理我的json反序列化。Json.net無法反序列化一個複雜類型的列表

首先,這裏是我嘗試使用的對象。

[DataContract] 
public class CreateSamplesRequest 
{ 
    [DataMember] public Guid SessionId { get; set; } 
    [DataMember] public SampleTemplateDTO Template { get; set; } 
} 
[DataContract] 
public class SampleTemplateDTO 
{ 
    [DataMember] public int NumberOfSamples { get; set; } 
    [DataMember] public int CompanyId { get; set; } 
    [DataMember] public int SampleTypeId { get; set; } 
    [DataMember] public HmisDTO Hmis { get; set; } 
    [DataMember] public List<AttributeValueDTO> AttributeValues { get; set; } 
} 
[DataContract] 
public class AttributeValueDTO 
{ 
    [DataMember] public int AttributeId { get; set; } 
    [DataMember] public string AttributeName { get; set; } 
    [DataMember] public string Value { get; set; } 
} 

[DataContract] 
public class HmisDTO 
{ 
    [DataMember] public string Health { get; set; } 
    [DataMember] public string Flammability { get; set; } 
    [DataMember] public string Reactivity { get; set; } 
    [DataMember] public string Equipment { get; set; } 
} 

幫助頁面請求JSON以這種形式爲CreateSamplesRequest

{ 
"SessionId":"1627aea5-8e0a-4371-9022-9b504344e724", 
"Template":{ 
    "NumberOfSamples":2147483647, 
    "CompanyId":2147483647, 
    "SampleTypeId":2147483647, 
    "Hmis":{ 
     "Health":"String content", 
     "Flammability":"String content", 
     "Reactivity":"String content", 
     "Equipment":"String content" 
    }, 
    "AttributeValues":[{ 
     "AttributeId":2147483647, 
     "AttributeName":"String content", 
     "Value":"String content" 
    }] 
    } 
} 

這就是我實際發送:

{ 
"SessionId":"17aaec11-be28-4536-b5df-d98fbda91e91", 
"Template":{ 
    "NumberOfSamples":1, 
    "CompanyId":1, 
    "SampleTypeId":9, 
    "Hmis":{ 
     "Health":"2", 
     "Flammability":"0", 
     "Reactivity":"0", 
     "Equipment":"E", 
    }, 
    "AttributeValues":[ 
     {"AttributeId":1,"AttributeName":"Item No.","Value":"MN0002NG"}, 
     {"AttributeId":2,"AttributeName":"Lot No.","Value":"C4526"} 
    ] 
} 
} 

我遇到的問題SampleTemplateDTO對象的AttributeValues屬性總是被忽略。使用上面的代碼,它將是空的。如果我將其設爲空List<AttributeValueDTO>它將是一個空列表。幾個小時後,我一直對此感到震驚。

我試過創建一個服務,只需要一個List<AttributeValueDTO>,它工作正常。我試着爲AttributeValues創建一個包裝類,它仍然以null結束。我完全被難住了。有任何想法嗎?

回答

6

上帝的母親,我只是在一個失敗的COMMA上浪費了5個小時。 HMIS部分中的尾隨逗號顯然告訴json.net在該點停止解析。

當我提交此JSON:

{ 
"SessionId":"17aaec11-be28-4536-b5df-d98fbda91e91", 
"Template":{ 
    "NumberOfSamples":1, 
    "CompanyId":1, 
    "SampleTypeId":9, 
    "Hmis":{ 
    "Health":"2", 
    "Flammability":"0", 
    "Reactivity":"0", 
    "Equipment":"E" 
    },  
    "AttributeValues":[ 
    {"AttributeId":1,"AttributeName":"Item No.","Value":"MN0002NG"}, 
    {"AttributeId":2,"AttributeName":"Lot No.","Value":"C4526"} 
    ]  
} 
} 

一切都很正常。

相關問題