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 に答える