【问题标题】:Deserializing a dynamic json to a c# class将动态 json 反序列化为 c# 类
【发布时间】:2017-07-17 11:34:24
【问题描述】:

我有一个动态 json 数组,它本质上可以是动态的。我想反序列化成一个类。 “datatype”标签决定了C#类成员的数据类型

[{
  "model": "DeviceModel.DeviceInstance",
  "name": "My-Device",
  "variables": {
    "Variable1": {
      "SubVariable1": {
        "DataType": "Double",
        "Unit": "V",
        "High": "3.5",
        "Low": "3.2",
        "Nominal": "3.3"
      },
      "SubVariable2": {
        "DataType": "Double",
        "Unit": "A",
        "High": "10"
      }
    },
    "Variable2": {
      "DataType": "Int",
      "Unit": "bytes",
      "Max": "100000",
      "Low": "10000",
      "LowLow": "500"
    }
  },
  "properties": {
    "ConstantProperty": {
      "PropertyName": {
        "DataType": "String",
        "Value": "12-34561"
      }
    }
  }
}
]

【问题讨论】:

  • DataType 是一个字符串。没有不同的类型。 value 描述了不同的数据类型。您应该描述反序列化结果的样子以避免混淆。
  • 虽然数据类型是基于文本 bool、float 或 string 的字符串,但对象需要反序列化为特定类型
  • @VivekRao 这是什么意思?您如何将“测试”反序列化为布尔值?
  • @CamiloTerevinto Theae 是特定设备的属性,而不是确切值。根据这些输入,需要采取某些行动。
  • 那么请移除该示例(甚至不是有效的 JSON)并添加一个有意义的示例(不必是真实数据,只是表明您的意图)

标签: c# json


【解决方案1】:

我已经解决了您的问题,发现下面的解决方案效果很好 参考: 1.http://json2csharp.com/#

2.JavaScriptSerializer.Deserialize array

public class SubVariable1
    {
        public string DataType { get; set; }
        public string Unit { get; set; }
        public string High { get; set; }
        public string Low { get; set; }
        public string Nominal { get; set; }
    }

    public class SubVariable2
    {
        public string DataType { get; set; }
        public string Unit { get; set; }
        public string High { get; set; }
    }

    public class Variable1
    {
        public SubVariable1 SubVariable1 { get; set; }
        public SubVariable2 SubVariable2 { get; set; }
    }

    public class Variable2
    {
        public string DataType { get; set; }
        public string Unit { get; set; }
        public string Max { get; set; }
        public string Low { get; set; }
        public string LowLow { get; set; }
    }

    public class Variables
    {
        public Variable1 Variable1 { get; set; }
        public Variable2 Variable2 { get; set; }
    }

    public class PropertyName
    {
        public string DataType { get; set; }
        public string Value { get; set; }
    }

    public class ConstantProperty
    {
        public PropertyName PropertyName { get; set; }
    }

    public class Properties
    {
        public ConstantProperty ConstantProperty { get; set; }
    }

    public class RootObject
    {
        public string model { get; set; }
        public string name { get; set; }
        public Variables variables { get; set; }
        public Properties properties { get; set; }
    }

    class Stackoverflow
    {
        static void Main(string[] args)
        {

            string strJSONData = "Your JSON Data";// Either read from string of file source
            System.Web.Script.Serialization.JavaScriptSerializer jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            List<RootObject> objRootObjectList = jsSerializer.Deserialize<List<RootObject>>(strJSONData);
Console.ReadLine();
        }
    }

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    相关资源
    最近更新 更多