首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将JSON反序列化为IEnumerable<T>对象的错误

将JSON反序列化为IEnumerable<T>对象的错误
EN

Stack Overflow用户
提问于 2018-04-13 14:40:16
回答 4查看 1.2K关注 0票数 0

我得到的错误如下:

{“无法创建和填充列表类型ReasonCodeList. Path 'AppointmentReasonList.ReasonCodeList',第1行,位置162”}

在转换以下示例JSON数据时,我得到了这个错误:

代码语言:javascript
复制
{
    "ErrorCode":""
    ,"ErrorMessage":""
    ,"AppointmentReasonList":[
        {
            "ClassCode":"851"
            ,"ClassDescription":"newpat"
            ,"Active":true
            ,"IsPatientRelated":true
            ,"ReasonCodeList":[
                {
                    "Code":"851"
                    ,"Description":"Emergency New Patient Visit"
                    ,"Duration":15
                    ,"Active":true
                }
                ,{
                    "Code":"BH NEW"
                    ,"Description":"BH NEW"
                    ,"Duration":15
                    ,"Active":true
                }
                            ]
        }
        ,{
            "ClassCode":"ANE"
            ,"ClassDescription":"Anesthesia"
            ,"Active":true
            ,"IsPatientRelated":true
            ,"ReasonCodeList":[
                {
                    "Code":"123456"
                    ,"Description":"This is only a test"
                    ,"Duration":15
                    ,"Active":true
                }
                                ]
        }                   
                            ]
}

我创建了以下类来尝试匹配数据结构。

代码语言:javascript
复制
public class AppointmentReasonResponse
        {
            public string ErrorCode { get; set; }
            public string ErrorMessage { get; set; }
            public List<AppointmentReason> AppointmentReasonList { get; set; }
        }

        public class AppointmentReason
        {
            public string ClassCode { get; set; }
            public string ClassDescription { get; set; }
            public bool Active { get; set; }
            public bool IsPatientRelated { get; set; }
            public ReasonCodeList ReasonCodeList { get; set; }
        }

        public class ReasonCodeList:IEnumerable<ReasonCode>
        {
            public List<ReasonCode> ReasonCodeLi { get; set; }

            IEnumerator<ReasonCode> IEnumerable<ReasonCode>.GetEnumerator()
            {
                // Return the array object's IEnumerator.
                foreach (ReasonCode Reason in ReasonCodeLi)
                {
                    yield return Reason;
                }
            }

            public IEnumerator<ReasonCode> GetEnumerator()
            {
                // Return the array object's IEnumerator.
                return ReasonCodeLi.GetEnumerator();
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                // call the generic version of the method
                return this.GetEnumerator();
            }
        }

        public class ReasonCode
        {
            public string Code { get; set; }
            public string Description { get; set; }
            public int Duration { get; set; }
            public bool Active { get; set; }
        }

如果你已经做到了这一点,我为冗长的代码表示歉意,但我认为所有这些都是必要的细节。

最初,对于这个question,我得到了相同的错误。

当我通过添加ReasonCodeList类来调整类结构时,我开始得到新的错误。

最终目标是提取JSON中每个原因对象的每个代码和描述,并将它们作为SQL过程中的参数使用。

这将需要两个foreach循环,而ReasonCodeList自定义集合必须实现IEnumerable。

任何帮助都将不胜感激。我不熟悉IEnumerable接口及其周围的限制。

编辑:

作为对@Mr Hery的回应:当我试图按照您的建议构造AppointmentReason类时,会发生以下错误:

{“无法反序列化当前的JSON对象(例如,{\”ReasonCodeList\“:\”值“})为类型.NET‘,因为该类型需要一个JSON数组(例如,1,2,3)反序列化。\r\n要修复此错误,可以将JSON更改为JSON数组(例如,1,2,3),或者将反序列化类型更改为正常的.NET类型(例如,不像整数这样的原始类型,而不是可以从JSON对象反序列化的集合类型,如数组或列表)。还可以将JsonObjectAttribute添加到类型中,以强制它从JSON对象反序列化。\r\n nPath 'AppointmentReasonList.ReasonCodeList.Code',第1行,位置170。

我最初是用ReasonCodeList属性作为列表的一种类型来构造这个类的,因为它本质上就是这样的,但是得到了相同的错误。我试着用ReasonCodeList对象代替它,就像上一篇文章中引用的答案中所建议的那样。

当我进行更改时,这个问题最上面的错误是发生了什么。

编辑#2

针对@ Fajardo &他的建议,反序列化的AppointReasonResponse对象将用于:

代码语言:javascript
复制
    jarray = JsonConvert.DeserializeObject<AppointmentReasonResponse>(json);
    foreach (AppointmentReason ApptReason in jarray.AppointmentReasonList)
    {
        foreach (ReasonCode Reason in ApptReason)
        {
            AddInterfacePMReasonCode(PracticeID, Reason.Code, Reason.Description);
        }
    }

由于以下错误导致的类ReasonCode的属性:

Error 105 foreach语句不能对“AppointmentReason”类型的变量进行操作,因为“AppointmentReason”不包含“GetEnumerator”的公共定义

编辑3

正如@ Fajardo所指出的,Edit #2中的内部foreach循环查看的是AppointmentReason对象,而不是它的list属性。当代码被修复时,会返回一个错误,就像我在这篇文章中发布的那个错误一样。

新类对象和修改后的foreach循环:

代码语言:javascript
复制
public class AppointmentReasonResponse
        {
            public string ErrorCode { get; set; }
            public string ErrorMessage { get; set; }
            public AppointmentReasonList AppointmentReasonList { get; set; }
        }

        public class AppointmentReasonList : IEnumerable<AppointmentReason>
        {

            public List<AppointmentReason> AppointmentReasonLi { get; set; }

            IEnumerator<AppointmentReason> IEnumerable<AppointmentReason>.GetEnumerator()
            {
                foreach (AppointmentReason ApptReason in AppointmentReasonLi)
                {
                    yield return ApptReason;
                }
            }

            public IEnumerator<AppointmentReason> GetEnumerator()
            {
                return AppointmentReasonLi.GetEnumerator();
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                return this.GetEnumerator();
            }
        }

foreach (AppointmentReason ApptReason in jarray.AppointmentReasonList)
                {
                    foreach (ReasonCode Reason in ApptReason.ReasonCodeList)
                    {
                        AddInterfacePMReasonCode(PracticeID, Reason.Code, Reason.Description);
                    }
                }

收到的错误:

{“无法创建和填充列表类型AppointmentReasonList. Path 'AppointmentReasonList',第1行,位置59”}

EN

回答 4

Stack Overflow用户

发布于 2018-04-14 07:00:16

也许可以将AppointmentReason更改为:

代码语言:javascript
复制
    public class AppointmentReason
    {
        public string ClassCode { get; set; }
        public string ClassDescription { get; set; }
        public bool Active { get; set; }
        public bool IsPatientRelated { get; set; }
        public List<ReasonCode> ReasonCodeList { get; set; }
    }

更新

您的循环代码不正确。应该是这样的(注意AppReason.ReasonCodeList,而不是内部foreach循环中的AppReason ):

代码语言:javascript
复制
jarray = JsonConvert.DeserializeObject<AppointmentReasonResponse>(json);
foreach (AppointmentReason ApptReason in jarray.AppointmentReasonList)
{
    foreach (ReasonCode Reason in ApptReason.ReasonCodeList)
    {
        AddInterfacePMReasonCode(PracticeID, Reason.Code, Reason.Description);
    }
}

更新2

我不知道为什么您将AppointmentReasonResponse.AppointmentReasonList属性从List<AppointmentReason>类型更改为新类型AppointmentReasonList?但是,您似乎认为需要引入一个实现枚举器的属性。但你不需要它。.

所以你的课程应该是这样的:

代码语言:javascript
复制
    public class AppointmentReasonResponse
    {
        public string ErrorCode { get; set; }
        public string ErrorMessage { get; set; }
        public List<AppointmentReason> AppointmentReasonList { get; set; }
    }

    public class AppointmentReason
    {
        public string ClassCode { get; set; }
        public string ClassDescription { get; set; }
        public bool Active { get; set; }
        public bool IsPatientRelated { get; set; }
        public List<ReasonCode> ReasonCodeList { get; set; }
    }

    public class ReasonCode
    {
        public string Code { get; set; }
        public string Description { get; set; }
        public int Duration { get; set; }
        public bool Active { get; set; }
    }

然后,如我前面所述,反序列化并使用它。

票数 1
EN

Stack Overflow用户

发布于 2018-04-30 16:01:38

谢谢大家的建议。我在一个单独的论坛上发布了这个问题,并从codeproject.com的Eric那里得到了答案。

你可以在这里查看他的答案:https://www.codeproject.com/Questions/1241686/JSON-deserializer-cannot-create-populate-object

正如Eric和@ Fajardo所指出的,AppointmentReasonListReasonCodeList类都不需要实现IEnumerable{T},因为List{T}类型已经实现了接口。

出于某种原因,您必须定义从List{T}继承的类,并将该类用作集合类型,而不是仅仅定义类型List{T}的属性本身。

我不知道为什么会这样,因为这两种方法似乎都在做相同的事情,但是当使用第二种方法时,当您试图迭代生成的对象时,会得到以下错误:

Error 105 foreach语句不能对“AppointmentReason”类型的变量进行操作,因为“AppointmentReason”不包含“GetEnumerator”的公共定义

不知道这是为什么,但最终,埃里克林奇的回答提供了预期的结果。

票数 1
EN

Stack Overflow用户

发布于 2018-04-13 14:57:03

错误说cannot create and populate list是因为您在代码中缺少了List<>。以下是更正:

代码语言:javascript
复制
public class AppointmentReasonResponse
{
    public string ErrorCode { get; set; }
    public string ErrorMessage { get; set; }
    public List<AppointmentReason> AppointmentReasonList { get; set; }
}

public class AppointmentReason
{
    public string ClassCode { get; set; }
    public string ClassDescription { get; set; }
    public bool Active { get; set; }
    public bool IsPatientRelated { get; set; }
    public List<ReasonCodeList> ReasonCodeList { get; set; }
}

public class ReasonCodeList:IEnumerable<ReasonCode>
{
    public List<ReasonCode> ReasonCodeLi { get; set; }

    IEnumerator<ReasonCode> IEnumerable<ReasonCode>.GetEnumerator()
    {
        // Return the array object's IEnumerator.
        foreach (ReasonCode Reason in ReasonCodeLi)
        {
            yield return Reason;
        }
    }

    public IEnumerator<ReasonCode> GetEnumerator()
    {
        // Return the array object's IEnumerator.
        return ReasonCodeLi.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        // call the generic version of the method
        return this.GetEnumerator();
    }
}

public class ReasonCode
{
    public string Code { get; set; }
    public string Description { get; set; }
    public int Duration { get; set; }
    public bool Active { get; set; }
}

您的JSON返回数组数据,因此您需要将List<>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49819521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档