C#WebAPI返回JSON数据
我对C#完全是新手,我正在努力学习WebApi。我有C#WebAPI返回JSON数据,c#,json,api,asp.net-web-api,C#,Json,Api,Asp.net Web Api,我对C#完全是新手,我正在努力学习WebApi。我有DataAccess.csproj,它包含列表,如下所示: using System; using System.Collections.Generic; using DataAccess.BO; namespace DataAccess { public class PersonDataAccess { #region Data private static readonly List<
DataAccess.csproj
,它包含列表
,如下所示:
using System;
using System.Collections.Generic;
using DataAccess.BO;
namespace DataAccess
{
public class PersonDataAccess
{
#region Data
private static readonly List<Person> Data = new List<Person>
{
new Person
{
Id = 8,
GivenName = "Trinh",
FamilyName = "Montejano",
BossId = 3,
Title = "Tech Manager",
Gender = Gender.Unspecified,
DateOfBirth = DateTime.Parse("1966-09-27")
},
new Person
{
Id = 1,
GivenName = "Winfred",
FamilyName = "Fetzer",
BossId = null,
Title = "CEO",
Gender = Gender.Unspecified,
DateOfBirth = DateTime.Parse("1927-01-29")
},
new Person
{
Id = 2,
GivenName = "Erich",
FamilyName = "Dandrea",
BossId = 1,
Title = "VP of Marketing",
Gender = Gender.Male,
DateOfBirth = DateTime.Parse("1927-08-20")
},
};
#endregion
//TODO: Implement whatever methods are needed to access the data.
}
}
“Addresses”对象类似于PersonDataAccess
我的usercontroller.cs
是这样吗
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DataAccess;
namespace SrEngineer.Controllers
{
[RoutePrefix("api/v1/user")]
public class UserController : ApiController
{
}
}
到目前为止,我只能解决这个问题,如何通过用户Id获取整个JSON对象和JSON对象?这是一个如何从webapi控制器返回JSON响应的示例
public class UserController : ApiController
{
/// <summary>
/// Get all Persons
/// </summary>
/// <returns></returns>
[HttpGet]
// GET: api/User/GetAll
[Route("api/User/GetAll")]
public IHttpActionResult GetData()
{
return Ok(PersonDataAccess.Data);
}
/// <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)
{
PersonDataAccess person = PersonDataAccess.Data.FirstOrDefault(p => p.Id = id);
if (person != null)
{
return Ok(person);
}
else
{
return NotFound();
}
}
}
公共类用户控制器:ApiController
{
///
///集合所有人
///
///
[HttpGet]
//获取:api/User/GetAll
[路由(“api/User/GetAll”)]
公共IHttpActionResult GetData()
{
返回Ok(PersonDataAccess.Data);
}
///
///按身份证取人
///
///个人id
///
//GET:api/User/GetByID/5
[路由(“api/User/GetByID/{id}”)]
公共IHttpActionResult GetById(int id)
{
PersonDataAccess person=PersonDataAccess.Data.FirstOrDefault(p=>p.Id=Id);
if(person!=null)
{
返回Ok(个人);
}
其他的
{
返回NotFound();
}
}
}
在您的数据访问类中
class PersonDataAccess
{
...
public IList<Person> GetData (int id) => Data.FirstOrDefault(x => x.Id == id);
}
您熟悉
JsonConvert.SerializeObject()
方法吗?网上有很多关于如何做到这一点的例子。。你尝试过谷歌搜索初学者吗?@MethodMan为什么你要自己在ApicController中进行JSON转换?框架会为您处理这个问题。请检查此项,而不是返回datetime返回您的列表“如何通过用户Id获取整个JSON对象和JSON对象?”…您需要两个操作方法。一个返回整个用户列表,另一个接受用户ID作为输入,选择整个用户并将其作为单个对象返回。要将对象/列表作为JSON返回,您不需要在Web API中执行任何特殊操作,只需返回该对象,.NET将负责转换。请首先研究此问题:。它应该为你提供回答问题所需的模式。第一个代码示例中的“GetAllProducts()”和“GetProduct(int-id)”方法类似于您所描述的(获取所有用户并获取单个用户),这样更好。但是问题的“JSON Object by User Id”部分怎么办?您仍然应该在答案中添加文本解释,而不仅仅是原始代码转储。@YashKaranke您应该在发布之前添加解释。不是在@YashKaranke之后。您的静态数据没有地址。你们打算使用ef或类似的东西从数据库中获取数据吗?若你们使用的是json文件,你们可以像附加链接那个样做
class PersonDataAccess
{
...
public IList<Person> GetData (int id) => Data.FirstOrDefault(x => x.Id == id);
}
[HttpGet]
public IHttpActionResult GetData(int id)
{
var result = (new PersonDataAccess ()).GetData(id);
if (result == null)
return NotFound();
return Ok(result);
}