如何从 Json 获取所有对象到列表

How to get all Object from Json to List

我有 Json 来自 Xenforo getNodes 的代码 API :

我正在使用 NewtonSoft Json 反序列化到列表,但我不知道如何为此 json

定义 class

希望能得到大家的帮助

 {
  "count": 1,
  "nodes": {
    "1": {
      "node_id": 1,
      "title": "Main Category",
      "description": "",
      "node_name": null,
      "node_type_id": "Category",
      "parent_node_id": 0,
      "display_order": 1,
      "display_in_list": 1,
      "lft": 1,
      "rgt": 4,
      "depth": 0,
      "style_id": 0,
      "effective_style_id": 0
    },

  }
}

因为当您创建新节点时,计数可以从 Xenforo API 动态更改,所以我不知道该怎么做...

谢谢

根据 API document,"nodes" 元素不是标准的 json 数组格式,因此您可能需要像这样使用自定义 JsonConverter

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public class NodesConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(Node[]);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var nodes = new List<Node>();

        foreach (JProperty property in serializer.Deserialize<JObject>(reader).Properties())
        {
            var node = property.Value.ToObject<Node>();
            // parse names as node_number
            node.node_number = int.Parse(property.Name);
            nodes.Add(node);
        }

        return nodes.ToArray();
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
    }
}

然后你可以反序列化json文本如下:

public class XenNode
{
    public int count { get; set; }
    [JsonConverter(typeof(NodesConverter))]
    public Node[] Nodes { get; set; }
}
public class Node
{
    public int node_number { get; set; }
    public int node_id { get; set; }
    public string title { get; set; }
    public string description { get; set; }
    public string node_name { get; set; }
    public string node_type_id { get; set; }
    public int parent_node_id { get; set; }
    public int display_order { get; set; }
    public int display_in_list { get; set; }
    public int lft { get; set; }
    public int rgt { get; set; }
    public int depth { get; set; }
    public int style_id { get; set; }
    public int effective_style_id { get; set; }
}

// Deserialize
XenNode xenNode = JsonConvert.DeserializeObject<XenNode>(json);

最里面的JSON对象映射到一个简单的C#对象,称之为Node;您可以使用 http://json2csharp.com/ 为您生成此 class。 "nodes" 对象看起来有一组可变的属性,其名称是查找键,其值是先前定义的 Node。所有这些都包含在一个根对象中。

因此:

public class Node
{
    public int node_id { get; set; }
    public string title { get; set; }
    public string description { get; set; }
    public object node_name { get; set; }
    public string node_type_id { get; set; }
    public int parent_node_id { get; set; }
    public int display_order { get; set; }
    public int display_in_list { get; set; }
    public int lft { get; set; }
    public int rgt { get; set; }
    public int depth { get; set; }
    public int style_id { get; set; }
    public int effective_style_id { get; set; }
}

public class RootObject
{
    public int count { get; set; }
    public Dictionary<string, Node> nodes { get; set; }
}