我目前正面临着一个挑战,即反序列化从Umbraco CMS返回的包含动态字段的JSON对象。
从Umbraco返回的json对象是高度可定制的,可能有这样的例子。
{
"content": {
"DataType": null,
"header": {
"title": "",
"logo": null,
"navigation": []
},
"footer": {
"Name" : "this is the footer"
"logo": null,
"Links": 0.0,
"copyRight": ""
}
}
字符串
或者更复杂的东西
{
"overview": "<h2>Content Overview</h2><p><a href=\"https://\">Text</a></p>",
"isVisible": false,
"description": "describe your product",
"bulletItems": [
"settings": null,
"content":{
"item": "confidential service",
"contentType": {
"key": "123",
"id": 1111,
"alias": "item",
"itemType": "Element",
"properties": [
{
"referenceCacheLevel": "Element",
"propertyType": {
"contentType": {
"key": "3234234",
"id": 1112,
"alias": "bulletItem",
"itemType": "Element",
"compositionAliases": [],
"variations": "Nothing",
"propertyTypes": [],
"isElement": true
}
}
}
]
}
}
]
}
型
具体地,接收到的对象可以包括或可以不包括诸如页眉、页脚、图标、链接、标题、内容等的字段。我的目标是反序列化这个对象,并将其放入一个标准结构(该结构涵盖了字段,数组和我们需要的对象)。如果它在我的类中具有该属性(同名),则将其反序列化并填充字段。如果它没有相关属性,则将其留空。从本质上讲,导入的JSON对象将作为数据源,并且所需的结果将是一个符合标准化结构的对象,其中所有必要的元素都进行了相应的过滤。
例如,下面是我定义的结构:
public class MyContentClass
{
public Header header;
public Footer footer;
public string title;
...
}
public class Header
{
public string name;
public int height;
public List<property> properties;
...
}
public class Footer
{
public string name;
public string content1;
public string content2;
public List<property> properties;
...
}
...
型
任何意见/建议将不胜感激。
1条答案
按热度按时间cxfofazt1#
你可以通过
using System.Text.Json
来实现。参考下面的代码片段:
字符串
参考工作样品here。