我需要将一些 JSON 解析为 C# 中的对象。我看过 Newtonsoft 和 JavaScriptSerializer,但要么我不知道如何很好地使用它们,要么它们在处理可能发生变化的格式方面做得很差,而且对于复杂的结构来说很尴尬。我想要一些我可以做的事情:
JsonObject j = Deserialize(mystring);
String[] earthColors = j.maps["earth"].colors;
并且不关心结构的其余部分。
最佳答案
我认为你应该重新考虑不使用 Json.Net
string mystring =
@"
{
""maps"": {
""earth"": {
""colors"": [
""blue"",
""green""
]
},
""moon"": {
""colors"": [
""black"",
""white""
]
}
}
";
dynamic j = JsonConvert.DeserializeObject(mystring);
foreach (var c in j.maps["earth"].colors)
{
Console.WriteLine(c);
}
关于c# - 在哪里可以找到适用于 C# 的简单但灵活的 JSON 解析器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10658669/