C# - JsonSerializer - DataContract运行时错误

时间:2015-08-11 09:21:06

标签: c# json rest serialization datacontractjsonserializer

我呼吁使用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) 我有invalidDataContractException WFEsura.Result不能被seralized。

描述是: System.Runtime.Serialization.dll中出现未处理的“System.Runtime.Serialization.InvalidDataContractException”类型异常

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中我已

    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您必须标记所有类和属性:

[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; }
}