为了账号安全,请及时绑定邮箱和手机立即绑定

如何反序列化C#中具有相同元素集合的Json元素

如何反序列化C#中具有相同元素集合的Json元素

C#
达令说 2022-12-24 12:41:48
我在 json 中有一个元素集合,里面有与嵌套元素集合相同的元素集合,所以如何在 c# 中反序列化我已经试过了。我创建了一个 C# 元素类,其属性也是同一类集合的属性//C# Codeclass pages{    public System.Collections.ObjectModel.Collection<elements> elements { get; set; }    public string name { get; set; }}class elements{    public string type { get; set; }    public string name { get; set; }    public string title { get; set; }    public System.Collections.ObjectModel.Collection<choices> choices { get; set; }    public System.Collections.ObjectModel.Collection<elements> element { get; set; }}//My Json{"pages": [ {  "name": "page1",  "elements": [  { "type": "panel", "name": "Q1", "elements": [  {   "type": "text",   "name": "Q2",   "title": "What is your age ?"  },  {   "type": "radiogroup",   "name": "QHidden",   "choices": [    {     "value": "1",     "text": "<18"    },    {     "value": "2",     "text": "18-30"    },    {     "value": "3",     "text": "30-50"    },    {     "value": "4",     "text": ">50"     }     ]     }    ]    }   ]  }  ]  }
查看完整描述

1 回答

?
杨魅力

TA贡献1811条经验 获得超6个赞

我创建了一个单独的具有相同属性的元素类


   class elements

   {

     public string type { get; set; }

     public string name { get; set; }

     public string title { get; set; }


     public System.Collections.ObjectModel.Collection<choices> choices { get; set; }


     public System.Collections.ObjectModel.Collection<elements2> element { get; set; 

     }

   }


   class elements2

   {

     public string type { get; set; }

     public string name { get; set; }

     public string title { get; set; }


     public System.Collections.ObjectModel.Collection<choices> choices { get; set; }

   }

这对我有用


查看完整回答
反对 回复 2022-12-24
  • 1 回答
  • 0 关注
  • 95 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号