c# - JsonSerializer - DataContract运行时错误

本文关键字:运行时错误 DataContract JsonSerializer | 更新日期: 2023-09-27 18:07:20

我要求使用json rest web服务。

我的类是:


    using System.Collections.Generic;
    using System.Runtime.Serialization;
    namespace WFAEsura
    {
        [DataContract]
        class JsonResponse
        {
            [DataMember]
            public string status { get; set; }
            [DataMember]
            public Result result { get; set; }
        }
        class Result
        {
            public string studentStatus { get; set; }
            public List<Results> results { get; set; }
        }
        class Results
        {
            public string code { get; set; }
            public string description { get; set; }
            public double creditAmount { get; set; }
            public int timeUnit { get; set; }
            public string mark { get; set; }
            public bool passed { get; set; }
            public string staffMemberName { get; set; }
            public List<Results> subResults { get; set; }
        }
    }

用于创建我使用的这些类http://json2csharp.com/
我的主要类是

<>之前 var syncClient = new WebClient(); string content = syncClient.DownloadString(baseUrl); // Create the Json serializer and parse the response DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(JsonResponse)); using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(content))) { // deserialize the JSON object var jsonResponse = (JsonResponse)serializer.ReadObject(ms); } 之前

但是在jsonResponse = (JsonResponse)serializer.ReadObject(ms)行我已经invaliddataconcontractexception WFEsura。结果不能序列化。

描述是:类型为"System.Runtime.Serialization"的未处理异常。InvalidDataContractException'在system . runtime . serialize .dll中发生

Additional information: Type 'WFAEsura.Result' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework documentation for other supported types.

在App.config中设置

<>之前& lt; startup>& lt;/startup>之前

我做错了什么?

c# - JsonSerializer - DataContract运行时错误

你必须标记所有的类和属性:

[DataContract]
class JsonResponse
{
    [DataMember]
    public string status { get; set; }
    [DataMember]
    public Result result { get; set; }
}
[DtaContract]
class Result
{
    [DataMember]
    public string studentStatus { get; set; }
    [DataMember]
    public List<Results> results { get; set; }
}
[DtaContract]
class Results
{
    [DataMember]
    public string code { get; set; }
    [DataMember]
    public string description { get; set; }
    [DataMember]
    public double creditAmount { get; set; }
    [DataMember]
    public int timeUnit { get; set; }
    [DataMember]
    public string mark { get; set; }
    [DataMember]
    public bool passed { get; set; }
    [DataMember]
    public string staffMemberName { get; set; }
    [DataMember]
    public List<Results> subResults { get; set; }
}