简体   繁体   中英

C# - JsonSerializer - DataContract runtime error

I call for to consume json rest web service.

My classes are:


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

        }
    }

For to create these classes I've used http://json2csharp.com/
My main class is



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

But in line jsonResponse = (JsonResponse)serializer.ReadObject(ms) i've invalidDataContractException WFEsura.Result cann't be seralized.

Description is: An unhandled exception of type 'System.Runtime.Serialization.InvalidDataContractException' occurred in System.Runtime.Serialization.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.

In App.config i've

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

What am I doing wrong?

You must mark all classes and properties:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM