C# 使用 Automapper 转换为 DTO/JSON
C# conversion to DTO/JSON using Automapper
我正在尝试使用 Automapper 从 API return 这种格式的 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;}
}
问题出在主题内部,每个主题都有一个来自对象内部 CourseName 的名称。
根据 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
}
}
]
}
我正在尝试使用 Automapper 从 API return 这种格式的 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;}
}
问题出在主题内部,每个主题都有一个来自对象内部 CourseName 的名称。
根据 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
}
}
]
}