序列化对键值对的动态响应 C#
Serialize dynamic response to key value pair C#
我正在尝试从内部 API 检索响应,并且响应中的对象之一具有动态字段。像这样。
{
"className": "grade12",
"School": "peg school",
"attributes": {
"propA": ["a1","a2","a3"],
"propB": ["b1","b2","b3",
"propC": ["c1","c2","c3"],
.
.
.
"propZ": ["1","2","3"]
},
"Create": "2022-01-31"
}
在属性字段中,可以有任意数量的字段。它可以为空或具有 30 个值。
我想序列化像
这样的东西
public class Fields
{
public string Name { get; set; }
public List<string> Value { get; set; }
}
public class Parent
{
public string className { get; set; }
public string School { get; set; }
public List<Fields> AllFields { get; set; }
public string Create { get; set; }
}
所以当我连载时我会有
{
"class": "grade12",
"School": "peg school",
"AllFields": [
{
"Name": "propA",
"value": ["a1","a2","a3"]
},
{
"Name": "propA",
"value": ["a1","a2","a3"]
}
],
"Create": "2022-01-31"
}
那么我该如何实现呢?
您可以通过基于您的父对象 class 创建一个 DTO 对象来轻松实现这一点,您可以在其中将字典转换为自定义对象列表。像这样:
public class Base
{
public string className { get; set; }
public string School { get; set; }
public string Create { get; set; }
}
public class Field
{
public string Name { get; set; }
public List<string> Value { get; set; }
public Field(KeyValuePair<string, List<string>> pair)
{
Name = pair.Key;
Value = pair.Value;
}
}
public class Parent : Base
{
public Dictionary<string, List<string>> attributes { get; set; }
}
public class ParentDTO : Base
{
public List<Field> AllFields { get; set; }
public ParentDTO(Parent parent)
{
className = parent.className;
School = parent.School;
Create = parent.Create;
AllFields = parent.attributes.Select(i => new Field(i)).ToList<Field>();
}
}
然后你可以序列化 ParentDTO 对象,你应该得到你想要的输出。
我正在尝试从内部 API 检索响应,并且响应中的对象之一具有动态字段。像这样。
{
"className": "grade12",
"School": "peg school",
"attributes": {
"propA": ["a1","a2","a3"],
"propB": ["b1","b2","b3",
"propC": ["c1","c2","c3"],
.
.
.
"propZ": ["1","2","3"]
},
"Create": "2022-01-31"
}
在属性字段中,可以有任意数量的字段。它可以为空或具有 30 个值。 我想序列化像
这样的东西public class Fields
{
public string Name { get; set; }
public List<string> Value { get; set; }
}
public class Parent
{
public string className { get; set; }
public string School { get; set; }
public List<Fields> AllFields { get; set; }
public string Create { get; set; }
}
所以当我连载时我会有
{
"class": "grade12",
"School": "peg school",
"AllFields": [
{
"Name": "propA",
"value": ["a1","a2","a3"]
},
{
"Name": "propA",
"value": ["a1","a2","a3"]
}
],
"Create": "2022-01-31"
}
那么我该如何实现呢?
您可以通过基于您的父对象 class 创建一个 DTO 对象来轻松实现这一点,您可以在其中将字典转换为自定义对象列表。像这样:
public class Base
{
public string className { get; set; }
public string School { get; set; }
public string Create { get; set; }
}
public class Field
{
public string Name { get; set; }
public List<string> Value { get; set; }
public Field(KeyValuePair<string, List<string>> pair)
{
Name = pair.Key;
Value = pair.Value;
}
}
public class Parent : Base
{
public Dictionary<string, List<string>> attributes { get; set; }
}
public class ParentDTO : Base
{
public List<Field> AllFields { get; set; }
public ParentDTO(Parent parent)
{
className = parent.className;
School = parent.School;
Create = parent.Create;
AllFields = parent.attributes.Select(i => new Field(i)).ToList<Field>();
}
}
然后你可以序列化 ParentDTO 对象,你应该得到你想要的输出。