NewtonSoft Json转换列表

我有一个非常简单的结构,我想使用牛顿软 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);


我究竟做错了什么?该示例表明这是可能的...


温温酱
浏览 119回答 1
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;    }}
打开App,查看更多内容
随时随地看视频慕课网APP