【问题标题】:C# conversion to DTO/JSON using Automapper使用 Automapper 将 C# 转换为 DTO/JSON
【发布时间】:2020-01-29 17:42:42
【问题描述】:

我正在尝试使用 Automapper 从 API 返回这种格式的 JSON:

  {
    "Name": "Jason",
    "Subjects":
      [
        "Maths":{
                  "CourseName": "Maths",
                  "Score": 70
                },
      "English":{
                  "CourseName": "English",
                  "Score": 80
                }
      ]
  }

这是一个特殊的 json,我们可以在其中看到 Subjects 每个主题的名称实际上是 CoursName 属性。这是我的模型和 DTO:

public class Student
{
    public string Name {get; set;}
    public ICollection Subjects {get; set;}
}

public class Subject
{
    public int Id {get; set;}
    public string CourseName {get; set;}
  public int Score {get; set;}
}

问题出在 Subjects 内部,每个主题都有一个来自于对象内部 CourseName 的 Name。

【问题讨论】:

  • 使用 Json.Net 就像在公园里散步,为什么要使用 automapper?
  • 显示您的 API 方法,或您如何进行转换。
  • @ZoharPeled 我们如何使用 Json.Net 实现这一目标?
  • 您只需序列化 Student 类,您可能希望在 Subject 类的 Id 属性上放置一个 [JsonIgnore] 属性。
  • 哦,我现在看到了问题所在。对不起,我的回答错了。

标签: c# .net automapper dto


【解决方案1】:

根据jsonformatter.curiousconcept.com,您提供的 JSON 无效。

此代码适用于有效的 JSON 输出 (RFC 8259)。

using System.Collections.Generic;
using Newtonsoft.Json;

namespace JsonProgram
{
    public class Program
    {
        static void Main(string[] args)
        {
            var student = new Student
            {
                Name = "Jason",
                Subjects = new List<Subject>
                {
                    new Subject
                    {
                        Id = 1,
                        CourseName = "Maths",
                        Score = 70
                    },
                    new Subject
                    {
                        Id = 2,
                        CourseName = "English",
                        Score = 80
                    },
                }
            };
            var json = ToStudentJson(student);
        }

        private static string ToStudentJson(Student student)
        {
            var subjects = new List<Dictionary<string, SubjectBase>>();
            foreach (var subject in student.Subjects)
            {
                var dict = new Dictionary<string, SubjectBase>();
                dict.Add(subject.CourseName, new SubjectBase { CourseName = subject.CourseName, Score = subject.Score });
                subjects.Add(dict);
            }

            var studentJson = new StudentJson
            {
                Name = student.Name,
                Subjects = subjects.ToArray()
            };

            return JsonConvert.SerializeObject(studentJson);
        }
    }

    public class StudentJson
    {
        public string Name { get; set; }
        public Dictionary<string, SubjectBase>[] Subjects { get; set; }
    }
    public class Student
    {
        public string Name { get; set; }
        public ICollection<Subject> Subjects { get; set; }
    }

    public class Subject : SubjectBase
    {
        public int Id { get; set; }
    }

    public class SubjectBase
    {
        public string CourseName { get; set; }
        public int Score { get; set; }
    }
}

输出:

{
    "Name": "Jason",
    "Subjects": [
        {
            "Maths": {
                "CourseName": "Maths",
                "Score": 70
            }
        },
        {
            "English": {
                "CourseName": "English",
                "Score": 80
            }
        }
    ]
}

【讨论】:

  • 感谢@Joel 的出色工作。给定的 json 只是一个示例。
猜你喜欢
  • 2016-06-08
  • 2014-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多