简体   繁体   English

在 Azure Modile App Service Backend 中将字符串属性反序列化为 Json Object

[英]Deserialize string Property as Json Object in Azure Modile App Service Backend

I'm having a similar problem as posted here Except I'm using an Azure Mobile App Service Backend.我遇到了与此处发布的类似问题,除了我使用的是 Azure 移动应用服务后端。 I have json string stored in the sql database and I need the App Service to de-serialize it to the object type.我在 sql 数据库中存储了 json 字符串,我需要应用服务将其反序列化为 object 类型。

Currently the Json from the Azure Mobile App Service Get looks like this: (and this isn't the in-code formatting, it's actually formatting the json that way)目前来自 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 is shown as a string an not a json nested object deviceSpeakerTapValue 显示为字符串而非 json 嵌套 object

I need the Json to look like this:我需要 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}"}

Since I'm using Azure Mobile App Service I don't know if the problem is with the Entity Framework or the Json Serializing/DeSerializing.由于我使用的是 Azure 移动应用服务,我不知道问题出在实体框架还是 Json 序列化/反序列化。 I also don't know how to change it since Azure Mobile App Service is a wrapper so you can't do things normally.我也不知道如何更改它,因为 Azure 移动应用服务是一个包装器,所以你不能正常做事。

Here is my EF object model and the model of what I need the string property to deserialize to:这是我需要将字符串属性反序列化为的 EF object model 和 model :

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; }
    }

sorry I can't comment yet.抱歉,我还不能发表评论。

I noticed in your second string you only capitalized the last 3 "items" in the json.我注意到在您的第二个字符串中,您仅将 json 中的最后 3 个“项目”大写。 Do you not care if the previous are capped?你不在乎前面有没有封顶吗?

Your String Above你上面的字符串

{"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}"}

Verification Question String验证问题字符串

{"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}"}

I am actually surprised your first values, such as updatedAt get deserialized properly.我实际上对您的第一个值感到惊讶,例如 updatedAt 得到正确反序列化。

What I would do is in your class definitions, add the JsonProperty attribute so that json knows to deserialize the non-backed parameter (such as Wattage doesn't have a private wattage property)我要做的是在您的 class 定义中,添加 JsonProperty 属性,以便 json 知道反序列化非支持参数(例如 Wattage 没有私有 wattage 属性)

So put所以放

public class TapValue
{
    [JsonProperty("value")]
    public double Value { get; set; }

    [JsonProperty("wattage")]
    public int Wattage { get; set; }

    [JsonProperty("direct")]
    public bool Direct { get; set; }
}

Are you using Newtonsoft?您在使用 Newtonsoft 吗? Because the above would tell it to map the lower case versions to the Upper case versions.因为上面会告诉 map 小写版本到大写版本。 Or if you had MyValueHere, you might have myValueHere in json, so make that your jsonproperty value myValueHere and it will map to MyValueHere或者如果你有 MyValueHere,你可能在 json 中有 myValueHere,所以让你的 jsonproperty 值为 myValueHere,它将 map 到 MyValueHere

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM