c# - 如何反序列化 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"
}
]
}
]
}
]
}
]
}
解决方案
我创建了一个具有相同属性的单独元素类
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; }
}
这对我有用
推荐阅读
- sql - 创建一个数据集,指示要在 Crystal Report 中显示的区域
- powershell - 使用 Powershell 进行队列邮箱导出
- javascript - 将 SQLite PHP 数组转换为 Javascript 数组?
- javascript - 为什么这个返回状态是未定义的?
- html - CSS将子菜单与父元素对齐
- mysql - 如何从不同的表中获取 MySQL 中最后插入的行?
- xml - 此时 %%X 出乎意料
- javascript - 更改 JSON 格式以具有适当的隐藏/显示键
- ios - 在标签上打印 instgram 回复时出现问题
- android - 使用 mqtt 发送推送通知,从最近的应用程序中滑动时不起作用