0

我呼吁使用 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 不能被序列化。

描述是: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 我有

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

我究竟做错了什么?

4

1 回答 1

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; }
}
于 2015-08-11T09:23:17.960 回答