- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个非常简单的结构,我想使用 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/
编辑:澄清问题: 我已经覆盖了基类型的 JsonConverter(通过将 [JsonConverter(typeof(TConverter))] 应用于父类(super class)),但是当直接反
尝试使用 JsonConvert.PopulateObject 更新类属性时,未调用 JsonPathConverter,因此未完成填充。 示例类: [JsonConverter(typeof(Jso
我有一个我不能改变的类: public enum MyEnum { Item1 = 0, Item2 = 1 } public class foo { [JsonConvert
我正在使用 Json.NET (6.0.3) 进行序列化。我编写了一个自定义 JsonConverter TestConverter 来处理一些 BaseClass 的实例(通过将它们转换为整数 id
我有一个要求,我需要将默认值设置为下面的复杂属性 Instances使用 JsonProperty和 DefaultValue . 我知道我们可以为以下链接中提到的原始属性实现这一点,但需要知道我们如
我尝试查找上述比较,但找不到答案。 因为有多种方法可以获取 JObject(或所有继承自 JToken 的子类型)例如: Method1 . JObject.FromObject(obj); Meth
我已启用我的 API 以使用字符串值序列化/反序列化枚举。为此,我已将 JsonStringEnumConverter 添加到 API 的 Startup 类中受支持的 JsonConverter 列
我有一个 Employee 类,其中 Manager 属性本身是 Employee 类型 public class Employee { public string FirstName { g
我已经卡住模型(简化): part 'initial_data_model.freezed.dart'; part 'initial_data_model.g.dart'; @freezed clas
我正在尝试将两个字段和一个包含两个字段的捆绑对象发布到 Mailchimp 的 API 端点。 var store_id = ConfigurationManager.AppS
以下VB.NET代码使用Newtonsoft JSON.NET库,并在按钮单击事件处理程序中执行并阻止UI线程: Dim table As DataTable = Await Task.Factory
我遇到了一个问题,其中以下 ToJson() 方法返回的字符串仅为“{}” public class GenericRequest { publ
这个问题在这里已经有了答案: JSON Can't be Deserialized to Object, Needs Array? (2 个答案) 关闭 9 年前。 我有这个 Json 返回: [
我遇到了一个问题,其中以下 ToJson() 方法返回的字符串仅为“{}” public class GenericRequest { publ
这个问题在这里已经有了答案: JSON Can't be Deserialized to Object, Needs Array? (2 个答案) 关闭 9 年前。 我有这个 Json 返回: [
我创建了一个简单的类: public class TestObject { public TestObject(int id, string name, List list) {
我有一个像这样的 Json 对象: {"company": "My Company", "companyStart" : "2015/01/01", "employee" : { "name"
这是 json: { "odata.metadata": ".....", "value": [ { "AbsEntry": 10,
我有一个对象,其中包含一个具有 JsonConverter 属性的属性。对于数据读写,转换器不工作。声明的属性是 [JsonConverter(typeof(EpochDateTimeConverte
编辑:制作了一个更简单、更透明的示例案例 我正在尝试反序列化一组组件(属于一个实体)。其中一个组件是 Sprite 组件,它包含纹理和动画信息。我为此实现了一个 CustomConverter,因为原
我是一名优秀的程序员,十分优秀!