Sql Web API如何通过HttpPost调用从数据库返回嵌套的JSON值?

Sql Web API如何通过HttpPost调用从数据库返回嵌套的JSON值?,sql,asp.net-web-api,Sql,Asp.net Web Api,我没有任何问题,但我不知道API如何调用数据嵌套值 我的Web API具有控制器、模型和数据访问权限,我使用sql调用这些控制器、模型和数据访问权限来调用数据 我希望API调用JSON数据格式如下: [ { "Agent_Code": "123456", "Name": "Miss Sara Manger", "NickName": "Sara", "BirthDay": "19690825", "Car

我没有任何问题,但我不知道API如何调用数据嵌套值

我的Web API具有控制器、模型和数据访问权限,我使用sql调用这些控制器、模型和数据访问权限来调用数据

我希望API调用JSON数据格式如下:

[
      {
        "Agent_Code": "123456",
        "Name": "Miss Sara Manger",
        "NickName": "Sara",
        "BirthDay": "19690825",
        "CardID": "9999999999",
        "Address": "870  Goldleaf Lane Lyndhurst NJ New Jersey 07071",
        "Mobile": "000000000",
        "Email": "utv9hgn3h0k@classesmail.com",
        "Bank": [
                      {
                        "AcctName": "Miss Sara Manger",
                        "Acctno": "9999999999",
                        "Bank": "KBANK"
                    }
                ]
        }
    ]
控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Testdata.APIs
{
    public class DashController : BaseApiController

    {
    [HttpPost]
       public List<Models.AgentDto> TestJSON(Models.CountDashReq model)
            {

                DataAccess.DashDAL dal = new DataAccess.DashDAL();
                List<Models.AgentDto> models = dal.TestJSON(model);

                return models;
            }
}
数据访问:

    public List<Models.AgentDto> TestJSON(Models.CountDashReq model)
    {
        string sql = "[dbo].[tb_Json_Get]";
        List<Models.AgentDto> result = new List<Models.AgentDto>();

        if (model != null)
        {

            List<SqlParameter> reqParam = new List<SqlParameter>();

            //reqParam.Add(new SqlParameter("@usrLogin", model.usrLogin));

            DataSet ds = this.Execute(sql, CommandType.StoredProcedure, reqParam.ToArray(), false);

            if (ds.Tables.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    Models.AgentDto paramslist = DataAccessUtility.DataTableExtensions.AsEnumerable<Models.AgentDto>(row);

                    result.Add(paramslist);
                }
            }
        }

        return result;
    }
public List TestJSON(Models.CountDashReq model)
{
字符串sql=“[dbo].[tb_Json_Get]”;
列表结果=新列表();
如果(型号!=null)
{
List reqParam=新列表();
//reqParam.Add(新的SqlParameter(“@usrLogin”,model.usrLogin));
DataSet ds=this.Execute(sql,CommandType.StoredProcess,reqParam.ToArray(),false);
如果(ds.Tables.Count>0)
{
foreach(ds.Tables[0].行中的DataRow行)
{
Models.AgentTo paramslist=DataAccessUtility.DataTableExtensions.AsEnumerable(行);
结果。添加(参数列表);
}
}
}
返回结果;
}

如果您收到请求JSON,请在非常感谢时帮助我使用该用例

[
  {
    "Agent_Code": "123456",
    "Name": "Miss Sara Manger",
    "NickName": "Sara",
    "BirthDay": "19690825",
    "CardID": "9999999999",
    "Address": "870  Goldleaf Lane Lyndhurst NJ New Jersey 07071",
    "Mobile": "000000000",
    "Email": "utv9hgn3h0k@classesmail.com",
    "Bank": [
      {
        "AcctName": "Miss Sara Manger",
        "Acctno": "9999999999",
        "Bank": "KBANK"
      }
    ]
  }
]
然后,
CountDashReq
类应为:

public class Bank
{
    public string AcctName { get; set; }
    public string Acctno { get; set; }
    public string Bank { get; set; }
}

public class CountDashReq
{
    public string Agent_Code { get; set; }
    public string Name { get; set; }
    public string NickName { get; set; }
    public string BirthDay { get; set; }
    public string CardID { get; set; }
    public string Address { get; set; }
    public string Mobile { get; set; }
    public string Email { get; set; }
    public List<Bank> Bank { get; set; }
}
公共类银行
{
公共字符串AcctName{get;set;}
公共字符串Acctno{get;set;}
公共字符串库{get;set;}
}
公共类CountDashReq
{
公共字符串代理_代码{get;set;}
公共字符串名称{get;set;}
公共字符串昵称{get;set;}
公共字符串生日{get;set;}
公共字符串CardID{get;set;}
公共字符串地址{get;set;}
公共字符串Mobile{get;set;}
公共字符串电子邮件{get;set;}
公共列表库{get;set;}
}
在控制器方法中:

[HttpPost]
public List<Models.AgentDto> TestJSON(IList<Models.CountDashReq> model)
{
  // Your code
}
[HttpPost]
公共列表TestJSON(IList模型)
{
//你的代码
}

如果您将请求JSON作为:

[
  {
    "Agent_Code": "123456",
    "Name": "Miss Sara Manger",
    "NickName": "Sara",
    "BirthDay": "19690825",
    "CardID": "9999999999",
    "Address": "870  Goldleaf Lane Lyndhurst NJ New Jersey 07071",
    "Mobile": "000000000",
    "Email": "utv9hgn3h0k@classesmail.com",
    "Bank": [
      {
        "AcctName": "Miss Sara Manger",
        "Acctno": "9999999999",
        "Bank": "KBANK"
      }
    ]
  }
]
然后,
CountDashReq
类应为:

public class Bank
{
    public string AcctName { get; set; }
    public string Acctno { get; set; }
    public string Bank { get; set; }
}

public class CountDashReq
{
    public string Agent_Code { get; set; }
    public string Name { get; set; }
    public string NickName { get; set; }
    public string BirthDay { get; set; }
    public string CardID { get; set; }
    public string Address { get; set; }
    public string Mobile { get; set; }
    public string Email { get; set; }
    public List<Bank> Bank { get; set; }
}
公共类银行
{
公共字符串AcctName{get;set;}
公共字符串Acctno{get;set;}
公共字符串库{get;set;}
}
公共类CountDashReq
{
公共字符串代理_代码{get;set;}
公共字符串名称{get;set;}
公共字符串昵称{get;set;}
公共字符串生日{get;set;}
公共字符串CardID{get;set;}
公共字符串地址{get;set;}
公共字符串Mobile{get;set;}
公共字符串电子邮件{get;set;}
公共列表库{get;set;}
}
在控制器方法中:

[HttpPost]
public List<Models.AgentDto> TestJSON(IList<Models.CountDashReq> model)
{
  // Your code
}
[HttpPost]
公共列表TestJSON(IList模型)
{
//你的代码
}

使用类似Jackson的库,帮助您从json序列化/反序列化json。网上有一些关于杰克逊的教程使用类似Jackson的库来帮助您从json序列化/反序列化到json。网上有一些关于杰克逊的教程-