本文介绍了映射两个列表并将其作为JSON返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试构建一个RESTFul API,我想将两个不同的列表映射在一起并将它们作为JSON对象返回。问题是用户有多个地址和地址存储在不同的文件中,用户详细信息存储在不同的文件中。  PersonDataAccess.cs 使用System; 使用System.Collections.Generic; 使用System.Linq; 使用System.Threading.Tasks; 使用DataAccess.BO; 命名空间DataAccess {公共类PersonDataAccess { #region数据公共静态只读列表<用户>数据=新列表<用户> {新用户 { Id = 8, GivenName =" Trinh", FamilyName =" Montejano", BossId = 3, Title =" Tech Manager", Gender = Gender.Unspecified, DateOfBirth = DateTime.Parse(" 1966-09-27")} ,新用户 { Id = 1, GivenName =" Winfred", FamilyName =" Fetzer", BossId = null, Title =" CEO", Gender = Gender.Unspecified, DateOfBirth = DateTime.Parse(" 1927-01-29")},新用户 { Id = 2, GivenName =" Erich", FamilyName =&q uot; Dandrea", BossId = 1, Title =" Marketing of Marketing", Gender = Gender.Male, DateOfBirth = DateTime.Parse(" 1927- 08-20")},新用户 { Id = 3, GivenName =" Reinaldo", FamilyName =" Nisbet" ;, BossId = 1, Title =""技术副总裁",性别=性别。男性, DateOfBirth = DateTime.Parse(" 1929-02-07" ;)},新用户 { Id = 4, GivenName =" Alleen", FamilyName =" Bufford", BossId = 1, Title =" HR of HR" Gender = Gender.Unspecified, Dat eOfBirth = DateTime.Parse(" 1932-06-13")}, new User { Id = 5, GivenName =" Kristyn" , FamilyName =" Klopfer", BossId = 2, Title =" Marketing of Marketing", Gender = Gender.Female, DateOfBirth = DateTime .Parse(" 1936-09-26")},新用户 { Id = 6, GivenName =" Sophie", FamilyName =" Duhon", BossId = 3, Title =" Tech Manager", Gender = Gender.Male, DateOfBirth = DateTime.Parse(" ; 1937-11-23")},新用户 { Id = 7, GivenName = "Suanne", FamilyName =" Mirabal", BossId = 3, Title =" Tech Manager", Gender = Gender.Female, DateOfBirth = DateTime.Parse(" 1948-04-05")}, new User { Id = 9, GivenName =" Norah" , FamilyName =" Maslowski", BossId = 4, Title =" Tech Manager", Gender = Gender.Unspecified, DateOfBirth = DateTime。 Parse(" 1966-10-13")}, new User { Id = 10, GivenName =" Gertrudis", FamilyName =" Redford", BossId = 6, Title =" Tech Lead",性别=性别。女性, DateOfBirth = DateTime.Parse(" 1967-08-25")},新用户 { Id = 11, GivenName =" Donovan", FamilyName =" Tobey", BossId = 6, Title =" Tech Lead", Gender = Gender。男, DateOfBirth = DateTime.Parse(" 1968-12-26")},新用户 { Id = 12, GivenName =" Rich", FamilyName =" Vermeulen", BossId = 9, Title =" Trainer Lead", Gender = Gender.Male, DateOfBirth = DateTime.Parse(" 1969-10-16")},新用户 { Id = 13, GivenName =" Santo", FamilyName =" Knupp", BossId = 9, Title =" HR Manager",性别=性别。男性, DateOfBirth = DateTime.Parse(" 1972-10-16")},新用户 { Id = 14, GivenName =" Jazmin", FamilyName =" Grooms", BossId = 12, Title =" Trainer", Gender = Gender.Female , DateOfBirth = DateTime.Parse(" 1974-03-23")}, new User { Id = 15, GivenName =" Annelle", FamilyName =" Cheeks", BossId = 13, Title =" ;招聘人员",性别=性别。女性, DateOfBirth = DateTime.Parse(" 1978-08-25")},新用户 { Id = 16, GivenName =" Eliza", FamilyName =" Harshaw", BossId = 12, Title =" Trainer",性别=性别。未指定, DateOfBirth = DateTime.Parse(" 1979-08-21")},新用户 { Id = 17, GivenName =" Xiomara", FamilyName =" Broaddus", BossId = 8, Title =" Senior Software Developer",性别=性别。未指定, DateOfBirth = DateTime.Parse(" 1980-02-09")},新用户 { Id = 18, GivenName =" Erminia", FamilyName =" Jungers", BossId = 11, Title =" Software Developer", Gender = Gender.Unspecified, DateOfBirth = DateTime.Parse(" 1981-09-08")}, new User { Id = 19, GivenName =" Maria", FamilyName =" Moffatt", BossId = 10, Title =" ; Software Developer", Gender = Gender.Female, DateOfBirth = DateTime.Parse(" 1984-03-18")}, new User { Id = 20, GivenName =" Tammera", FamilyName = "Grimaldo", BossId = 10, Title =" Senior Software Developer", Gender = Gender.Female, DateOfBirth = DateTime.Parse(" 1990- 09-24")},新用户 { Id = 21, GivenName =" Sharyl", FamilyName =" Das" ;, BossId = 10, Title =" Software Developer", Gender = Gender.Female, DateOfBirth = DateTime.Parse(" 1992-06-18" )},新用户 { Id = 22, GivenName =" Shan", FamilyName =" Harlan", BossId = 8, Title =" UI Developer", Gender = Gender.Unspecified, DateOfBirth = DateTime.Parse(" 1993-11-15")}, new User { Id = 23, GivenName =" Mariah" , FamilyName =" Almeida", BossId = 11, Title =" QA Tester", Gender = Gender.Female, DateOfBirth = DateTime。 Parse("1997-03-23")},新用户 { Id = 24, GivenName =" Darnell", FamilyName =" Kerfien", BossId = 11, Title =" QA Tester", Gender = Gender.Male, DateOfBirth = DateTime.Parse(" 1998-11-10")},新用户 { Id = 25, G ivenName =" Janell", FamilyName =" Vierra", BossId = 11, Title =" QA Tester", Gender = Gender.Female, DateOfBirth = DateTime.Parse(" 2004-04-22")} }; #endregion } } StreetAddressData.cs 使用System.Collections.Generic; 使用System.Linq; 使用DataAccess.BO; 命名空间DataAccess {公共类StreetStreetAddressData { #region private static readonly List< StreetAddress>地址=新列表< StreetAddress> { new StreetAddress {Id = 1,PersonId = 1,Street =" 62 Durham Court",City =" Garfield",State =" NJ",Zip =" 07026" }, new StreetAddress {Id = 2,PersonId = 1,Street =" 179 Cambridge Court" City =" Chippewa Falls",State =" WI",Zip =" 54729" }, new StreetAddress {Id = 3,PersonId = 1,Street =" 573 Route 5",City =" Memphis",State =" TN",Zip =" 38106" }, new StreetAddress {Id = 4,PersonId = 2,Street =" 173 Monroe Street",City =" Powhatan",State =" VA",Zip =" 23139" }, new StreetAddress {Id = 5,PersonId = 5,Street =" 47 Brookside Drive",City =" Westport",State =" CT",Zip =" 06880" }, new StreetAddress {Id = 6,PersonId = 6,Street =" 628 Creekside Drive",City =" Mankato",State =" MN",Zip =" 56001" }, new StreetAddress {Id = 7,PersonId = 7,Street =" 581 Lexington Court",City =" Sykesville",State =" MD",Zip =" 21784" }, new StreetAddress {Id = 8,PersonId = 11,Street =" 860 Deerfield Drive",City =" Muskego",State =" WI",Zip =" 53150" }, new StreetAddress {Id = 9,PersonId = 13,Street =" 189 Elizabeth Street" City =" Ashtabula",State =" OH",Zip =" 44004" }, new StreetAddress {Id = 10,PersonId = 13,Street =" 945 Rosewood Drive",City =" Fairfield",State =" CT",Zip =" 06824" }, new StreetAddress {Id = 11,PersonId = 13,Street =" 958 Hill Street",City =" Roy",State =" UT",Zip =" 84067" }, new StreetAddress {Id = 12,PersonId = 14,Street =" 687 Westminster Drive",City =" Desoto",State =" TX",Zip =" 75115" }, new StreetAddress {Id = 13,PersonId = 15,Street =" 530 Forest Drive",City =" Mc Lean",State =" VA",Zip =" 22101" }, new StreetAddress {Id = 14,PersonId = 17,Street =" 766 Cambridge Road",City =" Portage",State =" IN",Zip =" 46368" }, new StreetAddress {Id = 15,PersonId = 17,Street =" 788 Ivy Court",City =" Sunnyside",State =" NY",Zip =" 11104" }, new StreetAddress {Id = 16,PersonId = 19,Street =" 750 Ashley Court",City =" Selden",State =" NY",Zip =" 11784" }, new StreetAddress {Id = 17,PersonId = 20,Street =" 612 Harrison Street",City =" Winter Haven",State =" FL",Zip =" 33880" }, new StreetAddress {Id = 18,PersonId = 22,Street =" 780 Locust Lane",City =" Saint Petersburg",State =" FL",Zip =" 33702" }, new StreetAddress {Id = 19,PersonId = 22,Street =" 896 Chestnut Street",City =" Tallahassee",State =" FL",Zip =" 32303" }, new StreetAddress {Id = 20,PersonId = 24,Street =" 321 Roosevelt Avenue",City =" Dunedin",State =" FL",Zip =" 34698" },}; #endregion } } UserContrller.cs 使用System; 使用System.Collections.Generic; 使用System.Linq; 使用System.Net; 使用System.Net.Http; 使用System.Web.Http; 使用DataAccess; namespace SrEngineer.Controllers { public class UserController:ApiController { ///< summary> ///通过ID获取人物 ///< / summary> ///< param name =" id"> Person id< / param> ///< returns>< / returns> // GET:api / User / GetByID / 5 [路线(&api / User / GetByID / {id}")] public IHttpActionResult GetById(int id) { DataAccess.BO.User person = PersonDataAccess.Data.FirstOrDefault(p => p.Id == id); if(person!= null) { return Ok(person); } 其他 {返回NotFound(); } } } } 对不起基本问题,但如何我可以实现这个JSON架构吗? {" User":{" Id" :"1"," FirstName" :"Winfred"," LastName" :"Fetzer"," BossName" :null," Title" :"CEO"," DateOfBirth" :"1927-01-29","性别" :"女性","b $ b""地址" :[{" Id" :1," Street" :"62 Durham Court"," City" :"加菲猫","州" :"NJ"," Zip" :"07026": },{" Id" :2," Street" :"179 Cambridge Court","City"; :"Chippewa Falls","State" :"WI"," Zip" :"54729" },{" Id" :3," Street" :"573 Route 5"," City" :"孟菲斯","州" :"TN"," ZipCode" :"38106": }] } } 解决方案 可能其中一个简单的解决方案是定义另一个 用户类(可能在不同的命名空间中)对应于所需的JSON,例如: 类用户 {    public int Id;    公共字符串FirstName;   &NBSP; 公共字符串姓氏;   &NBSP; 。 。 。    public StreetAddress []地址; }   然后从 person 变量中创建此类对象。同时填写地址成员:   你 ser.Addresses = StreetStreetAddressData.Addresses.Where(a => a.PersonId == person.Id)。ToArray();   要从 StreetAddress 中排除 PersonId ,请使用 [NonSerialised]等属性, [ScriptIgnore] , [JsonIgnore] ,或省略 [DataMember] ,具体取决于您实施的某些细节。 $ b I'm trying to build a RESTFul API and I want to map Two different Lists together and return them as JSON Objects. The thing is Users have multiple Address and Addresses are stored in a Different file and User details are stored in a different file. PersonDataAccess.csusing System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using DataAccess.BO;namespace DataAccess{ public class PersonDataAccess { #region Data public static readonly List<User> Data = new List<User> { new User { Id = 8, GivenName = "Trinh", FamilyName = "Montejano", BossId = 3, Title = "Tech Manager", Gender = Gender.Unspecified, DateOfBirth = DateTime.Parse("1966-09-27") }, new User { Id = 1, GivenName = "Winfred", FamilyName = "Fetzer", BossId = null, Title = "CEO", Gender = Gender.Unspecified, DateOfBirth = DateTime.Parse("1927-01-29") }, new User { Id = 2, GivenName = "Erich", FamilyName = "Dandrea", BossId = 1, Title = "VP of Marketing", Gender = Gender.Male, DateOfBirth = DateTime.Parse("1927-08-20") }, new User { Id = 3, GivenName = "Reinaldo", FamilyName = "Nisbet", BossId = 1, Title = "VP of Technology", Gender = Gender.Male, DateOfBirth = DateTime.Parse("1929-02-07") }, new User { Id = 4, GivenName = "Alleen", FamilyName = "Bufford", BossId = 1, Title = "VP of HR", Gender = Gender.Unspecified, DateOfBirth = DateTime.Parse("1932-06-13") }, new User { Id = 5, GivenName = "Kristyn", FamilyName = "Klopfer", BossId = 2, Title = "Director of Marketing", Gender = Gender.Female, DateOfBirth = DateTime.Parse("1936-09-26") }, new User { Id = 6, GivenName = "Sophie", FamilyName = "Duhon", BossId = 3, Title = "Tech Manager", Gender = Gender.Male, DateOfBirth = DateTime.Parse("1937-11-23") }, new User { Id = 7, GivenName = "Suanne", FamilyName = "Mirabal", BossId = 3, Title = "Tech Manager", Gender = Gender.Female, DateOfBirth = DateTime.Parse("1948-04-05") }, new User { Id = 9, GivenName = "Norah", FamilyName = "Maslowski", BossId = 4, Title = "Tech Manager", Gender = Gender.Unspecified, DateOfBirth = DateTime.Parse("1966-10-13") }, new User { Id = 10, GivenName = "Gertrudis", FamilyName = "Redford", BossId = 6, Title = "Tech Lead", Gender = Gender.Female, DateOfBirth = DateTime.Parse("1967-08-25") }, new User { Id = 11, GivenName = "Donovan", FamilyName = "Tobey", BossId = 6, Title = "Tech Lead", Gender = Gender.Male, DateOfBirth = DateTime.Parse("1968-12-26") }, new User { Id = 12, GivenName = "Rich", FamilyName = "Vermeulen", BossId = 9, Title = "Trainer Lead", Gender = Gender.Male, DateOfBirth = DateTime.Parse("1969-10-16") }, new User { Id = 13, GivenName = "Santo", FamilyName = "Knupp", BossId = 9, Title = "HR Manager", Gender = Gender.Male, DateOfBirth = DateTime.Parse("1972-10-16") }, new User { Id = 14, GivenName = "Jazmin", FamilyName = "Grooms", BossId = 12, Title = "Trainer", Gender = Gender.Female, DateOfBirth = DateTime.Parse("1974-03-23") }, new User { Id = 15, GivenName = "Annelle", FamilyName = "Cheeks", BossId = 13, Title = "Recruiter", Gender = Gender.Female, DateOfBirth = DateTime.Parse("1978-08-25") }, new User { Id = 16, GivenName = "Eliza", FamilyName = "Harshaw", BossId = 12, Title = "Trainer", Gender = Gender.Unspecified, DateOfBirth = DateTime.Parse("1979-08-21") }, new User { Id = 17, GivenName = "Xiomara", FamilyName = "Broaddus", BossId = 8 , Title = "Senior Software Developer", Gender = Gender.Unspecified, DateOfBirth = DateTime.Parse("1980-02-09") }, new User { Id = 18, GivenName = "Erminia", FamilyName = "Jungers", BossId = 11, Title = "Software Developer", Gender = Gender.Unspecified, DateOfBirth = DateTime.Parse("1981-09-08") }, new User { Id = 19, GivenName = "Maria", FamilyName = "Moffatt", BossId = 10, Title = "Software Developer", Gender = Gender.Female, DateOfBirth = DateTime.Parse("1984-03-18") }, new User { Id = 20, GivenName = "Tammera", FamilyName = "Grimaldo", BossId = 10, Title = "Senior Software Developer", Gender = Gender.Female, DateOfBirth = DateTime.Parse("1990-09-24") }, new User { Id = 21, GivenName = "Sharyl", FamilyName = "Das", BossId = 10, Title = "Software Developer", Gender = Gender.Female, DateOfBirth = DateTime.Parse("1992-06-18") }, new User { Id = 22, GivenName = "Shan", FamilyName = "Harlan", BossId = 8, Title = "UI Developer", Gender = Gender.Unspecified, DateOfBirth = DateTime.Parse("1993-11-15") }, new User { Id = 23, GivenName = "Mariah", FamilyName = "Almeida", BossId = 11, Title = "QA Tester", Gender = Gender.Female, DateOfBirth = DateTime.Parse("1997-03-23") }, new User { Id = 24, GivenName = "Darnell", FamilyName = "Kerfien", BossId = 11, Title = "QA Tester", Gender = Gender.Male, DateOfBirth = DateTime.Parse("1998-11-10") }, new User { Id = 25, GivenName = "Janell", FamilyName = "Vierra", BossId = 11, Title = "QA Tester", Gender = Gender.Female, DateOfBirth = DateTime.Parse("2004-04-22") } }; #endregion }}StreetAddressData.csusing System.Collections.Generic;using System.Linq;using DataAccess.BO;namespace DataAccess{ public class StreetStreetAddressData { #region private static readonly List<StreetAddress> Addresses = new List<StreetAddress> { new StreetAddress{ Id = 1, PersonId = 1, Street = "62 Durham Court", City = "Garfield", State="NJ", Zip ="07026" }, new StreetAddress{ Id = 2, PersonId = 1, Street = "179 Cambridge Court", City = "Chippewa Falls", State="WI", Zip ="54729" }, new StreetAddress{ Id = 3, PersonId = 1, Street = "573 Route 5", City = "Memphis", State="TN", Zip ="38106" }, new StreetAddress{ Id = 4, PersonId = 2, Street = "173 Monroe Street", City = "Powhatan", State="VA", Zip ="23139" }, new StreetAddress{ Id = 5, PersonId = 5, Street = "47 Brookside Drive", City = "Westport", State="CT", Zip ="06880" }, new StreetAddress{ Id = 6, PersonId = 6, Street = "628 Creekside Drive", City = "Mankato", State="MN", Zip ="56001" }, new StreetAddress{ Id = 7, PersonId = 7, Street = "581 Lexington Court", City = "Sykesville", State="MD", Zip ="21784" }, new StreetAddress{ Id = 8, PersonId = 11, Street = "860 Deerfield Drive", City = "Muskego", State="WI", Zip ="53150" }, new StreetAddress{ Id = 9, PersonId = 13, Street = "189 Elizabeth Street", City = "Ashtabula", State="OH", Zip ="44004" }, new StreetAddress{ Id = 10, PersonId = 13, Street = "945 Rosewood Drive", City = "Fairfield", State="CT", Zip ="06824" }, new StreetAddress{ Id = 11, PersonId = 13, Street = "958 Hill Street", City = "Roy", State="UT", Zip ="84067" }, new StreetAddress{ Id = 12, PersonId = 14, Street = "687 Westminster Drive", City = "Desoto", State="TX", Zip ="75115" }, new StreetAddress{ Id = 13, PersonId = 15, Street = "530 Forest Drive", City = "Mc Lean", State="VA", Zip ="22101" }, new StreetAddress{ Id = 14, PersonId = 17, Street = "766 Cambridge Road", City = "Portage", State="IN", Zip ="46368" }, new StreetAddress{ Id = 15, PersonId = 17, Street = "788 Ivy Court", City = "Sunnyside", State="NY", Zip ="11104" }, new StreetAddress{ Id = 16, PersonId = 19, Street = "750 Ashley Court", City = "Selden", State="NY", Zip ="11784" }, new StreetAddress{ Id = 17, PersonId = 20, Street = "612 Harrison Street", City = "Winter Haven", State="FL", Zip ="33880" }, new StreetAddress{ Id = 18, PersonId = 22, Street = "780 Locust Lane", City = "Saint Petersburg", State="FL", Zip ="33702" }, new StreetAddress{ Id = 19, PersonId = 22, Street = "896 Chestnut Street", City = "Tallahassee", State="FL", Zip ="32303" }, new StreetAddress{ Id = 20, PersonId = 24, Street = "321 Roosevelt Avenue", City = "Dunedin", State="FL", Zip ="34698" }, };#endregion }}UserContrller.csusing System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Http;using System.Web.Http;using DataAccess;namespace SrEngineer.Controllers{ public class UserController : ApiController { /// <summary> /// Get Person By ID /// </summary> /// <param name="id">Person id </param> /// <returns></returns> // GET: api/User/GetByID/5 [Route("api/User/GetByID/{id}")] public IHttpActionResult GetById(int id) { DataAccess.BO.User person = PersonDataAccess.Data.FirstOrDefault(p => p.Id == id); if (person != null) { return Ok(person); } else { return NotFound(); } } }}Sorry for the basic question, but How can I achieve this JSON Schema?{"User": {"Id" : "1", "FirstName" : "Winfred", "LastName" : "Fetzer","BossName" : null, "Title" : "CEO", "DateOfBirth" : "1927-01-29", "Gender" : "Female", "Addresses" : [{"Id" : 1,"Street" : "62 Durham Court","City" : "Garfield","State" : "NJ","Zip" : "07026"},{"Id" : 2,"Street" : "179 Cambridge Court","City" : "Chippewa Falls","State" : "WI","Zip" : "54729"},{"Id" : 3,"Street" : "573 Route 5","City" : "Memphis","State" : "TN","ZipCode" : "38106"}]}} 解决方案 Probably one of the simple solutions is to define anotherUser class (maybe in a different namespace) that corresponds to required JSON, for example:class User{  public int Id;  public string FirstName;  public string LastName;  . . .   publicStreetAddress [ ] Addresses;} Then make such object from yourperson variable. Also fill the Addresses member:   user.Addresses = StreetStreetAddressData.Addresses.Where( a => a.PersonId == person.Id).ToArray( ); In order to exclude thePersonId from StreetAddress, use an attribute like [NonSerialised],[ScriptIgnore], [JsonIgnore], or omit [DataMember], depending on certain details of your implementation. 这篇关于映射两个列表并将其作为JSON返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-01 15:12