如何使用 YAMLDotNet 反序列化 YAML? [英] How to deserialize YAML with YAMLDotNet?

查看:55
本文介绍了如何使用 YAMLDotNet 反序列化 YAML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 YAMLDotNet 反序列化这个 YAML.
它有序列和嵌套映射.

I want to deserialize this YAML with YAMLDotNet.
It have sequence and nested mapping.

  • 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

这是我的代码.但是,它无法编译.我想知道以下两点.

This is my code. But, It can't compile. I would like to know the following two things.

  1. 如何定义类来反序列化嵌套映射?
  2. 如何访问它?

  • 打印反序列化数据
  • 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 的建议帮助我解决了问题.谢谢,

      @AluanHaddad's advice helped me solve my problem. Thanks,

      // 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆