简体   繁体   English

System.Text.Json 无法使用对象数组反序列化 object?

[英]System.Text.Json can't deserialize an object with an array of objects?

I have a json structure that looks like this:我有一个 json 结构,如下所示:

{
"SystemType": "SingleDevice",
"Devices":
[
    {
    "DeviceSettings":{
        "DeviceType": "Blah",
        "Generates": 4,
        "RAdd": 10,
        "TAdd": 10,
        "Distance": 1,
        "IpAddress": "192.168.0.21",
        "Port": 50002 
        }
    }
]}

I then have two classes that looks like this:然后我有两个看起来像这样的类:

DeviceSettings.cs设备设置.cs

public class DeviceSettings
{
    public string DeviceType { get; set; }
    public int Generates { get; set; }
    public double RAdd{ get; set; }
    public double TAdd { get; set; }
    public double Distance { get; set; }
    public string IpAddress { get; set; }
    public int Port { get; set; }
}

SystemSettings.cs系统设置.cs

  public class SystemSettings
{
    public string SystemType { get; set; }
    public DeviceSettings[] Devices { get; set; }
}

When I debug the following code and look at the locals, the resulting object that gets returned exists but with some issues.当我调试以下代码并查看局部变量时,返回的结果 object 存在但存在一些问题。 The SystemType properly displays "SingleDevice", and Devices indicates it's an array of size=1, and provides element 0, but when I look the actual values for everything, they are either null or 0. Specifically, DeviceType and IpAddress = null, everything else is 0. Can anyone explain why and how to get around this? SystemType 正确显示“SingleDevice”,Devices 表示它是一个大小为 1 的数组,并提供元素 0,但是当我查看所有内容的实际值时,它们要么是 null 要么是 0。具体来说,DeviceType 和 IpAddress = null,所有内容否则为 0。谁能解释为什么以及如何解决这个问题?

        static void Main(string[] args)
    {
        var configFilePath = @"C:\Users\Public\Documents\myFolder\SystemConfiguration.json";
        var config = System.IO.File.ReadAllText(configFilePath);
        Console.WriteLine(config);
        var systemSettings = JsonSerializer.Deserialize<SystemSettings>(config);
        Console.ReadKey();
    }

locals output当地人 output

I've seen posts talking about inferred types, but I believe everything is explicit here.我看过讨论推断类型的帖子,但我相信这里的一切都是明确的。 I'm not quite sure what's going on.我不太确定发生了什么。

You are missing an object to hold the DeviceSettings property:您缺少 object 来保存DeviceSettings属性:

public class SystemSettings
{
    public string SystemType { get; set; }
    public Device[] Devices { get; set; } // <-- change this
}

// Add this
public class Device
{
    public DeviceSettings DeviceSettings { get; set; }

}

public class DeviceSettings
{
    //snip
}

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

相关问题 JsonSerializer.反序列化<t> (); 在 System.Text.Json 中没有正确反序列化</t> - JsonSerializer.Deserialize<T>(); in System.Text.Json not deserialize correctly 使用 System.Text.Json 反序列化数组 json object 的嵌套数组 - Using System.Text.Json to deserialize nested array of array json object System.Text.Json 将多个子对象的object反序列化为同一个实例 - System.Text.Json Deserialize object with multiple child objects into the same instance 使用字典 System.Text.Json 将 json 反序列化为 object - deserialize json to object with a dictionary System.Text.Json System.Text.Json object 数组反序列化 - System.Text.Json object array deserialization 如何反序列化 ReadOnlySpan<char> 使用 System.Text.Json 到 object</char> - How to deserialize ReadOnlySpan<char> to object using System.Text.Json System.Text.Json 反序列化列表失败<list<object> > </list<object> - System.Text.Json fails to deserialize List<List<object>> System.Text.Json - 将嵌套对象反序列化为字符串 - System.Text.Json - Deserialize nested object as string 如何使用 System.Text.Json API 将 stream 反序列化为 object - How to deserialize stream to object using System.Text.Json APIs c# system.text.json 将“不同”对象反序列化为一个类的数组 - c# system.text.json deserialize "different" object to an array of one class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM