【问题标题】:C# - JsonSerializer - DataContract runtime errorC# - JsonSerializer - DataContract 运行时错误
【发布时间】:2015-11-03 10:45:30
【问题描述】:

我呼吁消费json rest web service。

我的课程是:


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

启动>

我做错了什么?

【问题讨论】:

    标签: c# json rest serialization datacontractjsonserializer


    【解决方案1】:

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

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

    【讨论】:

    • @SimoneMura 你可以接受我的回答,这将是公平的
    猜你喜欢
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多