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

我在 json 中有一个元素集合,里面有与嵌套元素集合相同的元素集合,所以如何在 c# 中反序列化


我已经试过了。我创建了一个 C# 元素类,其属性也是同一类集合的属性


//C# Code

class 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"

     }

     ]

     }

    ]

    }

   ]

  }

  ]

  }


达令说
浏览 189回答 1
1回答

杨魅力

我创建了一个单独的具有相同属性的元素类&nbsp; &nbsp;class elements&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp;public string type { get; set; }&nbsp; &nbsp; &nbsp;public string name { get; set; }&nbsp; &nbsp; &nbsp;public string title { get; set; }&nbsp; &nbsp; &nbsp;public System.Collections.ObjectModel.Collection<choices> choices { get; set; }&nbsp; &nbsp; &nbsp;public System.Collections.ObjectModel.Collection<elements2> element { get; set;&nbsp;&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}&nbsp; &nbsp;class elements2&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp;public string type { get; set; }&nbsp; &nbsp; &nbsp;public string name { get; set; }&nbsp; &nbsp; &nbsp;public string title { get; set; }&nbsp; &nbsp; &nbsp;public System.Collections.ObjectModel.Collection<choices> choices { get; set; }&nbsp; &nbsp;}这对我有用
打开App,查看更多内容
随时随地看视频慕课网APP