簡體   English   中英

如何使用自動映射器在json中映射類實體?

[英]How to map class entity within json with automapper?

我將元信息存儲到字符串字段中。 但是在DTO對象中,我映射了單獨的類對象。

樣本JSON Post對象

{

  "name": "string",
  "directionId": 0,
  "description": "string",
  "siteId": 0,
  "zoneId": 0,
  "areaId": 0,
  "metaData": {
    "input": {
      "name": "string",
      "key": "string",
      "agentId": 0,
      "type": 0
    },
    "outPut": {
      "name": "string",
      "key": "string",
      "agentId": 0,
      "type": 0
    },
    "position": {
      "lat": "string",
      "long": "string"
    }
  }
}

實體類對象

/// <summary>
/// SRC  
/// </summary>
public class SRC
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int? DirectionId { get; set; }
    public string Description { get; set; }
    public int? SiteId { get; set; }
    public int? ZoneId { get; set; }
    public int? AreaId { get; set; }

    /// <summary>
    /// Json view MetaData
    /// { 
    ///   "input": {
    ///      name:"my input1",
    ///       key:"43434",
    ///       agent:"1",
    ///       type:"1",
    ///    }
    ///    "output": {
    ///      name:"my output",
    ///       key:"12333",
    ///       agent:"1",
    ///       type:"1",
    ///    }
    ///    "position": {
    ///      lat:"42°21'30"N 71°03'37",
    ///       long:"42°21'30"S 71°03'37",
    ///    }
    /// }
    /// </summary>
    public string MetaData { get; set; }
}

DTO類對象

/// <summary>
/// SRC DTO
/// </summary>
public class SrcDTO
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int? DirectionId { get; set; }
    public string Description { get; set; }
    public int? SiteId { get; set; }
    public int? ZoneId { get; set; }
    public int? AreaId { get; set; }
    [JsonProperty("MetaData")]
    public SourceMetaData MetaData { get; set; }
}
#region Meta Data Class

public class SRCMetaData
{
    [JsonProperty("Input")]
    SourceInput SourceInput { get; set; }
    [JsonProperty("OutPut")]
    SourceOutput SourceOutput { get; set; }
    [JsonProperty("Position")]
    SourcePosition SourcePosition { get; set; }
}
public class SourceInput
{
    public string Name { get; set; }
    public string Key { get; set; }
    public int AgentId { get; set; }
    public int Type { get; set; }
}
public class SourceOutput
{
    public string Name { get; set; }
    public string Key { get; set; }
    public int AgentId { get; set; }
    public int Type { get; set; }
}
public class SourcePosition
{
    public string Lat { get; set; }
    public string Long { get; set; }
}
#endregion

如何與自動映射器配置文件映射?

   CreateMap<SrcDTO, Src>()
        .ForMember(x => x.MetaData, cfg => { cfg.MapFrom(jo => JsonConvert.SerializeObject(jo.MetaData)); })
        ;

        CreateMap<Src,  SrcDTO>()
        .ForMember(x=>x.MetaData , cfg => { cfg.MapFrom(jo => JsonConvert.DeserializeObject(jo.MetaData)); })
        ;

在發送JSON的后期操作中,它工作正常。 但是在GetALL操作中,它不起作用,請參見下面的圖片以供參考。 在此處輸入圖片說明 在此處輸入圖片說明

問題是在子類對象中映射“ Int”數據類型字段。 我已通過使用“字符串”數據類型字段進行修改來解決。

正如通過鑄造使用JsonConvert.SerializeObject序列化的,它對我有用。

謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM