【问题标题】:How to get all Object from Json to List如何将所有对象从 Json 获取到 List
【发布时间】:2016-02-18 08:07:45
【问题描述】:

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

我正在使用 NewtonSoft Json 反序列化到 List 但我不知道如何为这个 json 定义类

希望能得到大家的帮助

 {
  "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 动态变化,所以我不知道该怎么做...

谢谢

【问题讨论】:

    标签: c# json windows windows-runtime winrt-xaml


    【解决方案1】:

    最里面的 JSON 对象映射到一个简单的 C# 对象,称之为Node;您可以使用http://json2csharp.com/ 为您生成此类。 "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; }
    }
    

    【讨论】:

    • 谢谢...我正在使用 Windows.DAta.Json 和复杂的 Json.net 然后没问题!我会试试你的解决方案:)
    【解决方案2】:

    根据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);
    

    【讨论】:

      猜你喜欢
      • 2016-12-28
      • 2021-10-23
      • 1970-01-01
      • 2015-10-30
      • 2014-06-14
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      • 2021-09-24
      相关资源
      最近更新 更多