C#WebAPI返回JSON数据

时间:2018-12-17 21:27:13

标签: c# json api asp.net-web-api

我绝对不熟悉C#,并且正在尝试学习WebApi。我有DataAccess.csproj,其中包含List,如下所示:

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.
    }
}

并且我想返回原始JSON之类的数据

{
    "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"
        }]
    }
}

“地址”对象类似于PersonDataAccess

我的UserContrller.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对象?

2 个答案:

答案 0 :(得分:2)

这是一个如何从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();
        }
    }
}

https://www.glfw.org/docs/3.0/quick.html

答案 1 :(得分:-1)

在您的数据访问类中

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);
}