您尝试反序列化的类似乎缺少属性。我绕过了将 yaml 转换为 json 到 csharp 的方法,这是生成的类:
public class Rootobject
{
public Icon[] icons { get; set; }
}
public class Icon
{
public string[] categories { get; set; }
public object created { get; set; }
public string[] filter { get; set; }
public string id { get; set; }
public string name { get; set; }
public string unicode { get; set; }
public string[] aliases { get; set; }
public string[] label { get; set; }
public string[] code { get; set; }
public string url { get; set; }
}
使用的资源:
YAML to JSON online
JSON to CSHARP(我在visual studio中使用了Paste special)
使用它来反序列化
var icons = deserializer.Deserialize<RootObject>(input);
更新
我已经注释掉了用于创建 YamlStream 的行,因为它不是必需的(它将阅读器定位到流的末尾而不是开头,这可以解释为什么你之前得到 null)。您的主要方法如下所示并且有效。我还修复了 Antoine 提到的错误
public static void Main()
{
string filePath = "https://raw.githubusercontent.com/FortAwesome/Font-Awesome/master/src/icons.yml";
WebClient client = new WebClient();
string reply = client.DownloadString(filePath);
var input = new StringReader(reply);
//var yamlStream = new YamlStream();
//yamlStream.Load(input);
Deserializer deserializer = new Deserializer();
//var icons = deserializer.Deserialize<IconSearch>(input);
//Testing my own implementation
//if (icons == null)
// Console.WriteLine("Icons is null");
//Testing Shekhar's suggestion
var root = deserializer.Deserialize<Rootobject>(input);
if (root == null)
Console.WriteLine("Root is null");
}