NewtonSoft Json转换列表

Posted

技术标签:

【中文标题】NewtonSoft Json转换列表【英文标题】:NewtonSoft JsonConvert list 【发布时间】:2019-12-13 19:34:12 【问题描述】:

我有一个非常简单的结构,我想使用牛顿软 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 表明这是可能的......

【问题讨论】:

从 SensorConfiguration 中移除 [JsonArray] 属性 【参考方案1】:

尝试删除[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;
    

【讨论】:

嘿,这行得通。谢谢。知道什么时候应该使用该属性吗?文档指出:“JsonArrayAttribute/JsonDictionaryAttribute JsonArrayAttribute 和 JsonDictionaryAttribute 用于指定一个类是否被序列化为该集合类型” 你的类不是集合类型——你只能在集合类型上使用这个属性。但一般来说,您不需要使用 JSON 属性 - 保持类干净,仅在需要时使用它们

以上是关于NewtonSoft Json转换列表的主要内容,如果未能解决你的问题,请参考以下文章

Visual Studio中如何安装使用Newtonsoft Json

无法从字符串转换为 NewtonSoft.Json.JsonReader

如何从 Newton.Json.Linq.JToken 转换为 byte[]?

C# unity (发布到安卓端中使用)解析json字符串—使用微软官方的包Newtonsoft.Json

C# unity (发布到安卓端中使用)解析json字符串—使用微软官方的包Newtonsoft.Json

C# unity (发布到安卓端中使用)解析json字符串—使用微软官方的包Newtonsoft.Json