[英]Deserialize Yaml into c# collection
I have the following yaml structure: 我具有以下yaml结构:
TestPaySignService:
Test:
TestString:
settings-key: TestStringValue
types:
- type1
- type2
TestString2:
settings-key: TestStringValue2
types:
- type1
- type2
I want to parse this yaml and store only "settings-key" and "types" together in c# collection, for example List, like the following: 我想解析此Yaml,并将“ settings-key”和“ types”一起存储在c#集合中,例如List,如下所示:
[
{
name: TestStringValue,
types: ["type1", "type2"]
},
{
name: TestStringValue2,
types: ["type1", "type2"]
}
]
I've followed the follwin question and tried the accepted answer's code, but as it seems it's obsolete for now: Deserialize a YAML "Table" of data 我已经遵循了follwin问题并尝试了接受的答案的代码,但是目前看来它已经过时了: 反序列化数据的YAML“表”
PS my yaml schema changes every time, but "settings-key" and "types" are always contained, so I want to create something like "dynamic parser" PS我的Yaml模式每次都会更改,但是始终包含“设置键”和“类型”,因此我想创建类似“动态解析器”的内容
You can deserialize the yaml to a dynamic object then walk a bit. 您可以将Yaml反序列化为动态对象,然后再走一点。 Something like the following: 类似于以下内容:
public static void Main()
{
var r = new StringReader(@"TestPaySignService:
Test:
TestString:
settings-key: TestStringValue
types:
- type1
- type2
TestString2:
settings-key: TestStringValue2
types:
- type1
- type2");
var deserializer = new Deserializer();
var yamlObject = deserializer.Deserialize<dynamic>(r)["TestPaySignService"]["Test"].Values;
// just to print the json
var serializer = new JsonSerializer();
serializer.Serialize(Console.Out, yamlObject);
}
Output 输出量
[{
"settings-key": "TestStringValue",
"types": [
"type1",
"type2"
]
},
{
"settings-key": "TestStringValue2",
"types": [
"type1",
"type2"
]
}]
If you need to use TestString, you should deserialize it into an object. 如果需要使用TestString,则应将其反序列化为对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.