【问题标题】:deserialize then serialize json with dynamic field name反序列化然后用动态字段名序列化 json
【发布时间】:2021-12-28 16:30:26
【问题描述】:

我有一个类似这样的 JSON 文件。

{
  "id": "top1",
  "components": [
    {
      "type": "resistor",
      "id": "res1",
      "resistance": {
        "default": 100,
        "min": 10,
        "max": 1000
      },
      "netlist": {
        "t1": "vdd",
        "t2": "n1"
      }
    },
    {
      "type": "nmos",
      "id": "m1",
      "m(l)": {
        "default": 1.5,
        "min": 1,
        "max": 2
      },
      "netlist": {
        "drain": "n1",
        "gate": "vin",
        "source": "vss"
      }
    }
  ]
}

我想使用 oop 制作一个 API 来处理该 JSON 文件, 我做了以下课程。

public class Topology
    {
        [Required]
        public string id { get; set; }
        [Required]
        public List<TopologyComponents> components { get; set; }
    }
public class TopologyComponents
    {
        [Required]
        public string type { get; set; }
        [Required]
        public string id { get; set; }
        [Required]
        public Values ???????? {get; set; }
        [Required]
        public Dictionary<string, string> netlist { get; set; }
    }
public class Values
    {
        [Required]
        public double @default { get; set; }
        [Required]
        public double min { get; set; }
        [Required]
        public double max { get; set; }
    }

我的问题是那些问号?????? 字段名称是动态的resistance, m(l), .....
我该如何处理这些情况?
我尝试了 Jackson 注释、expandobject 和字典。但它们都没有按我的意愿工作。

【问题讨论】:

    标签: c# json oop json.net api-design


    【解决方案1】:

    从外观上看,您的Topology 类需要具有Dictionary&lt;string, dynamic&gt; 数据类型,因为components 的键是任意的。尽管type 和id 在所有组件中都相同,但netlist 和另一个属性将是动态的。

    将您的组件列表更改为Dictionary&lt;string, dynamic&gt;,然后通过首先检查组件中实际存在的属性来获取您需要的数据。

    public class Topology
    {
        [Required]
        public string id { get; set; }
        [Required]
        public List<Dictionary<string, dynamic>> components { get; set; }
    }
    

    这将为您提供一个字典列表,其中字符串作为键,动态对象作为值。您可以使用components.Keys 上的 foreach 循环遍历键,也可以使用 switch 语句来查看在遍历每个组件时是否存在您期望的键。

    有关如何创建自己的组件列表的示例...不确定您将如何使用数据,因为这将推动您反序列化的方式,

    var obj = JsonConvert.DeserializeObject<Topology>(jsonText);
    List<dynamic> allComps = new List<dynamic>();
    foreach(var component in obj.components)
    {
        var comp = new ExpandoObject() as IDictionary<string, object>;
        foreach(var key in component.Keys)
        {
            switch (key)
            {
                case "id":
                case "type":
                    comp.Add(key, component[key].ToString());
                    break;
    
                case "netlist":
                    comp.Add(key, component[key].ToObject<Dictionary<string, string>>());
                    break;
    
                default:
                    comp.Add(key, component[key].ToObject<Values>());
                    break;
            }
        }
        allComps.Add(comp);
    }
    

    【讨论】:

    • 你能详细说明一下吗,我无法让它为我工作。
    • @AloneTheGreat 为您的 Topology 类添加了代码块。
    • 结果为@​​987654321@
    • @AloneTheGreat 请参阅我的帖子中的编辑,了解获取组件的方法
    猜你喜欢
    • 2015-11-22
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 2019-12-24
    相关资源
    最近更新 更多