为了账号安全,请及时绑定邮箱和手机立即绑定

NewtonSoft Json转换列表

NewtonSoft Json转换列表

C#
温温酱 2023-08-13 15:59:07
我有一个非常简单的结构,我想使用牛顿软 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);我究竟做错了什么?该示例表明这是可能的...
查看完整描述

1 回答

?
森栏

TA贡献1810条经验 获得超5个赞

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

    }

}


查看完整回答
反对 回复 2023-08-13
  • 1 回答
  • 0 关注
  • 118 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信