我遇到与发布的here类似的问题除了我使用的是Azure移动应用服务后端。我将json字符串存储在sql数据库中,并且需要App Service将其反序列化为对象类型。
当前,来自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对象模型,以及我需要将string属性反序列化为的模型:
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; }
}
抱歉,我无法发表评论。