繁体   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