【发布时间】:2020-09-30 06:57:04
【问题描述】:
我遇到了与here 发布的类似问题 除了我使用的是 Azure 移动应用服务后端。 我将 json 字符串存储在 sql 数据库中,我需要应用服务将其反序列化为对象类型。
目前来自 Azure 移动应用服务 Get 的 Json 如下所示: (这不是代码内格式,它实际上是以这种方式格式化 json)
{"deleted": false,"updatedAt": "2020-06-09T16:30:48.09Z","createdAt": "2020-06-03T04:34:41.617Z","version": "AAAAAABXrYA=","id": "DBEC6DE9-3C5C-47C5-8404-67C79FCF6740","equipmentSpeakerTapValue": "{\"value\": 0.0,\"wattage\": 0,\"direct\": false}"}
equipmentSpeakerTapValue 显示为字符串而非 json 嵌套对象
我需要 Json 看起来像这样:
{"deleted": false,"updatedAt": "2020-06-09T16:30:48.09Z","createdAt": "2020-06-03T04:34:41.617Z","version": "AAAAAABXrYA=","id": "DBEC6DE9-3C5C-47C5-8404-67C79FCF6740","equipmentSpeakerTapValue":{"Value":2.5,"Wattage":70,"Direct":false}"}
由于我使用的是 Azure 移动应用服务,我不知道问题出在实体框架还是 Json 序列化/反序列化。我也不知道如何更改它,因为 Azure 移动应用服务是一个包装器,因此您无法正常执行操作。
这是我的 EF 对象模型和我需要将字符串属性反序列化为的模型:
public class SiteEquipment
{
public string Id { get; set; }
public byte[] Version { get; set; }
public DateTimeOffset? CreatedAt { get; set; }
public DateTimeOffset? UpdatedAt { get; set; }
public bool Deleted { get; set; }
//I don't know which EquipmentSpeakerTapValue to use:
//string version that serializes to: "equipmentSpeakerTapValue": "{\"value\": 0.0,\"wattage\": 0,\"direct\": false}"
public string EquipmentSpeakerTapValue { get; set; }
//object version that should serialize to: "equipmentSpeakerTapValue":{"Value":2.5,"Wattage":70,"Direct":false}"
public TapValue EquipmentSpeakerTapValue { get; set; }
}
public class TapValue
{
public double Value { get; set; }
public int Wattage { get; set; }
public bool Direct { get; set; }
}
【问题讨论】:
标签: c# json entity-framework azure-mobile-services