gpt4 book ai didi

c# - NewtonSoft JsonConvert 列表

转载 作者:太空宇宙 更新时间:2023-11-03 22:31:35 24 4
gpt4 key购买 nike

我有一个非常简单的结构,我想使用 newton soft Json 序列化进行序列化。

定义:

public enum SensorType
{
Temperature,
Flow,
Pressure
}

public enum SensorLocation
{
Manifold,
TopVessel,
WaferStage
}

[JsonArray]
public class SensorConfiguration
{
[JsonProperty]
public string Name { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public SensorType Type { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public SensorLocation Location { get; set; }

public SensorConfiguration()
{
}

public SensorConfiguration(string name, SensorType type, SensorLocation location)
{
Name = name;
Type = type;
Location = location;
}
}

序列化:

        var topvessel = Sensors.TopVessel.Select(sensor =>
new SensorConfiguration(sensor.SensorName, sensor.Type, SensorLocation.TopVessel));
var manifold = Sensors.Manifold.Select(sensor =>
new SensorConfiguration(sensor.SensorName, sensor.Type, SensorLocation.Manifold));
var waferstage = Sensors.WaferStage.Select(sensor =>
new SensorConfiguration(sensor.SensorName, sensor.Type, SensorLocation.Manifold));

var sensorConfigurations = topvessel.Concat(manifold).Concat(waferstage).ToList();

var json = JsonConvert.SerializeObject(sensorConfigurations);

错误:

System.InvalidCastException : Unable to cast object of type 'Asml.Mbi.FlowAndTemperature.Interfaces.Configuration.SensorConfiguration' to type 'System.Collections.IEnumerable'.
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)
at Newtonsoft.Json.JsonConvert.SerializeObject(Object value)
at Asml.Mbi.FlowAndTemperature.Peripherals.Ftcb.FtcBox.GetSensorConfiguration() in D:\dev\multibeaminspection\BuildingBlocks\FlowAndTemperature\Implementation\Peripherals\Ftcb\FtcBox.cs:line 75
at Asml.Mbi.FlowAndTemperature.UnitTest.FtcBoxTests.GetConfiguration() in D:\dev\multibeaminspection\BuildingBlocks\FlowAndTemperature\UnitTest\FtcBoxTests.cs:line 212

我做错了什么? example表明这是可能的...

最佳答案

尝试移除[JsonArray]

所以你的代码看起来像

public class SensorConfiguration
{
[JsonProperty]
public string Name { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public SensorType Type { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public SensorLocation Location { get; set; }

public SensorConfiguration()
{
}

public SensorConfiguration(string name, SensorType type, SensorLocation location)
{
Name = name;
Type = type;
Location = location;
}
}

关于c# - NewtonSoft JsonConvert 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57372793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com