简体   繁体   English

将JSON对象反序列化为.Net列表(C#)

[英]Deserialize JSON Objects to .Net List (C#)

I have a JSON-String (below). 我有一个JSON-String(如下)。 I want to deserialize it to a C# Object and get the data as a list. 我想将其反序列化为C#对象并以列表的形式获取数据。 My problem is, that the data is not available in an JSON array. 我的问题是,该数据在JSON数组中不可用。 How do I need to prepare my solution that I can get data in this structure: 我需要如何准备解决方案以获取以下结构的数据:

  • Sensors 传感器
    • Sensor 1 传感器1
    • Sensor 2 传感器2
    • Sensor 3 传感器3

For Sensor 1 - n I just need the GUIDs in a list. 对于传感器1-n,我只需要列表中的GUID。 The data behind is not relevant. 后面的数据无关紧要。

In the Code I replaced all GUIDs with "GUID" 在代码中,我将所有GUID替换为“ GUID”

{
  "object": {
    "GUID": {
      "type": "sensor",
      "owner": "GUID",
      "time": 1482499665,
      "description": "Temperatursensor 1",
      "sdevice": "00003639",
      "model": "SOLUCON Industry Temperature",
      "tag": [
        "GUID",
        "GUID"
      ]
    },
    "GUID": {
      "type": "sensor",
      "owner": "GUID",
      "time": 1482499758,
      "description": "Wassersensor 1",
      "sdevice": "000056d9",
      "model": "SOLUCON Industry Water",
      "tag": [
        "GUID",
        "GUID"
      ]
    },
    "GUID": {
      "type": "sensor",
      "owner": "GUID",
      "time": 1482499797,
      "description": "Rauchmelder 1",
      "sdevice": "00008519",
      "model": "TG551A",
      "tag": [
        "GUID",
        "GUID"
      ]
    },
    "GUID": {
      "type": "sensor",
      "owner": "GUID",
      "time": 1483888365,
      "description": "SOLUCON Industry Multi 2",
      "sdevice": "0000d409",
      "model": "SOLUCON Industry Multi",
      "tag": [
        "GUID",
        "GUID"
      ]
    }
  },
  "status": "ok"
}

You could use the Newtonsoft.Json package like so: 您可以像这样使用Newtonsoft.Json包:

var jsonString = ...
var result = JsonConvert.DeserializeObject<IDictionary<string, object>>(jsonString);
var obj = (JObject)result["object"];
foreach (var prop in obj.Properties())
{
    Console.WriteLine(prop.Name);
}

This will print all the GUID properties of the object node. 这将打印object节点的所有GUID属性。

And if you wanted to get the extra objects you could define a model: 如果您想获得额外的对象,则可以定义一个模型:

public class Item
{
    public string Type { get; set; }

    public Guid Owner { get; set; }

    public string Description { get; set; }

    public IList<string> Tag { get; set; }

    ...
}

and then you could get the sensor like this: 然后您可以像这样获得传感器:

foreach (var prop in obj.Properties())
{
    Console.WriteLine(prop.Name);

    Sensor sensor = prop.Value.ToObject<Sensor>();
}

Structure you provided looks like a dictionary so it should deserialize to: 您提供的结构看起来像字典,因此应反序列化为:

class Data
{
    public Dictionary<Guid, Sensor> Object {get;set;}
}

From this you can extract list of keys ( data.Object.Keys ) that will contain your list of Guids. 从中可以提取将包含data.Object.Keys列表的键列表( data.Object.Keys )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM