如何使用 YAMLDotNet 反序列化 YAML?
How to deserialize YAML with YAMLDotNet?
我想用 YAMLDotNet 反序列化这个 YAML。
它有序列和嵌套映射。
- data.yml
people:
- name: "John"
age: 20
- name: "Michael"
age: 21
- name: "William"
age: 22
network:
address: "192.168.1.1"
port: 1234
param:
paramNumber: 10
paramString: "text data"
paramBool: true
这是我的代码。但是,它无法编译。
我想知道以下两件事
- 如何定义 class 去定义嵌套映射?
- 如何访问?
- 打印反序列化数据
DeserializedObject obj = YamlImporter.Deserialize("data.yml");
foreach(var people in obj.people)
{
Console.WriteLine(people.name);
Console.WriteLine(people.age);
}
Console.WriteLine(obj.network["address"]);
Console.WriteLine(obj.network["port"]);
/* how to access param? */
- 解串器代码 (YAMLDotNet)
public class YamlImporter
{
public static DeserializedObject Deserialize(string yamlName)
{
StreamReader sr = new StreamReader(yamlName);
string text = sr.ReadToEnd();
var input = new StringReader(text);
var deserializer = new DeserializerBuilder().WithNamingConvention(CamelCaseNamingConvention.Instance).Build();
DeserializedObject deserializeObject = deserializer.Deserialize<DeserializedObject>(input); /* compile error */
return deserializeObject;
}
}
public class DeserializedObject
{
public List<People> people { get; set; }
public class People
{
public string name { get; set; }
public int age { get; set; }
}
public Dictionary<string, Network> network { get; set; }
public class Network
{
public string address { get; set; }
public int port { get; set; }
public Dictionary<string, Param> param { get; set; }
}
public class Param
{
public int paramNumber { get; set; }
public string paramString { get; set; }
public bool paramBool { get; set; }
}
}
谢谢,
@AluanHaddad 的建议帮助我解决了我的问题。谢谢,
// How to access params? //
Console.WriteLine(obj.network.param.paramNumber);
Console.WriteLine(obj.network.param.paramString);
Console.WriteLine(obj.network.param.paramBool);
//public Dictionary<string, Network> network { get; set; }
public Network network { get; set; }
public class Network
{
public string address { get; set; }
public int port { get; set; }
//public Dictionary<string, Param> param { get; set; }
public Param param { get; set; }
}
我想用 YAMLDotNet 反序列化这个 YAML。
它有序列和嵌套映射。
- data.yml
people:
- name: "John"
age: 20
- name: "Michael"
age: 21
- name: "William"
age: 22
network:
address: "192.168.1.1"
port: 1234
param:
paramNumber: 10
paramString: "text data"
paramBool: true
这是我的代码。但是,它无法编译。 我想知道以下两件事
- 如何定义 class 去定义嵌套映射?
- 如何访问?
- 打印反序列化数据
DeserializedObject obj = YamlImporter.Deserialize("data.yml");
foreach(var people in obj.people)
{
Console.WriteLine(people.name);
Console.WriteLine(people.age);
}
Console.WriteLine(obj.network["address"]);
Console.WriteLine(obj.network["port"]);
/* how to access param? */
- 解串器代码 (YAMLDotNet)
public class YamlImporter
{
public static DeserializedObject Deserialize(string yamlName)
{
StreamReader sr = new StreamReader(yamlName);
string text = sr.ReadToEnd();
var input = new StringReader(text);
var deserializer = new DeserializerBuilder().WithNamingConvention(CamelCaseNamingConvention.Instance).Build();
DeserializedObject deserializeObject = deserializer.Deserialize<DeserializedObject>(input); /* compile error */
return deserializeObject;
}
}
public class DeserializedObject
{
public List<People> people { get; set; }
public class People
{
public string name { get; set; }
public int age { get; set; }
}
public Dictionary<string, Network> network { get; set; }
public class Network
{
public string address { get; set; }
public int port { get; set; }
public Dictionary<string, Param> param { get; set; }
}
public class Param
{
public int paramNumber { get; set; }
public string paramString { get; set; }
public bool paramBool { get; set; }
}
}
谢谢,
@AluanHaddad 的建议帮助我解决了我的问题。谢谢,
// How to access params? //
Console.WriteLine(obj.network.param.paramNumber);
Console.WriteLine(obj.network.param.paramString);
Console.WriteLine(obj.network.param.paramBool);
//public Dictionary<string, Network> network { get; set; }
public Network network { get; set; }
public class Network
{
public string address { get; set; }
public int port { get; set; }
//public Dictionary<string, Param> param { get; set; }
public Param param { get; set; }
}