How Web API Return Nested JSON Values by HttpPost call from database?

Pewwy Meew

I dont have any problems but i don't know how API call data Nested Values

My Web API have Controller, Models and DataAccess that i call by use sql to call data

I want API call JSON Data format similar as below :

[
      {
        "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": "[email protected]",
        "Bank": [
                      {
                        "AcctName": "Miss Sara Manger",
                        "Acctno": "9999999999",
                        "Bank": "KBANK"
                    }
                ]
        }
    ]

Controller:

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

Model AgentDto And BankDto:

public class AgentDto
    {
        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 string Bank { get; set; }
    }

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

DataAccess:

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

Please help me to use Case-when Thank you very much

Prashant Pimpale

If you getting request JSON as:

[
  {
    "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": "[email protected]",
    "Bank": [
      {
        "AcctName": "Miss Sara Manger",
        "Acctno": "9999999999",
        "Bank": "KBANK"
      }
    ]
  }
]

Then the CountDashReq class should be:

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

In controller method:

[HttpPost]
public List<Models.AgentDto> TestJSON(IList<Models.CountDashReq> model)
{
  // Your code
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Return Nested Json from database as an array with Laravel

分類Dev

When you call a Web API method from jQuery, how jQuery knows what format to return?

分類Dev

How can I access elements of a JSON return from a Stripe api call?

分類Dev

How to return a JSON Web Token in a C# WEB API?

分類Dev

Accessing nested values in JSON data from Twitch API

分類Dev

How to display JSON values from API

分類Dev

How to call servicestack based web api from swift?

分類Dev

Extracting values from a nested JSON file

分類Dev

Retrieving values from an API call | Wix

分類Dev

How do i call the parsed data from the GET request that have a longer nested JSON structure in Swift?

分類Dev

Return specific values from array of hashes - JSON

分類Dev

How to return nested class pointer from method?

分類Dev

how to access nested Json key values in Golang

分類Dev

How to search into nested JSON by values with Python

分類Dev

How to store the values of a JSON response to an API Call in as many variables as the (unknown) number of objects using node.js

分類Dev

How to call an ASP.NET Core Web API endpoint from within separate Docker Containers

分類Dev

How to return a 304 result from ASP.NET Core web api?

分類Dev

How to get the return values from a function?

分類Dev

AutoMapper - how to return values from foreign key?

分類Dev

How to return values from a threaded watchservice

分類Dev

How to print all results of a JSON API call

分類Dev

How to create json object dynamically from UI using values from input fields before sending to API

分類Dev

How to populate jtable checkbox values from database?

分類Dev

Converting nested dict from json into dataframe with values as columns

分類Dev

Return "raw" json in ASP.NET Core 2.0 Web Api

分類Dev

Web API does not serialize bool return type as a json boolean value

分類Dev

Support both JSON and XML as return type in Web API 2

分類Dev

Angular 2 & .NET Core Web API HttpPost の問題

分類Dev

How to Return SQL FIeld Names in Javascript call to API?

Related 関連記事

  1. 1

    Return Nested Json from database as an array with Laravel

  2. 2

    When you call a Web API method from jQuery, how jQuery knows what format to return?

  3. 3

    How can I access elements of a JSON return from a Stripe api call?

  4. 4

    How to return a JSON Web Token in a C# WEB API?

  5. 5

    Accessing nested values in JSON data from Twitch API

  6. 6

    How to display JSON values from API

  7. 7

    How to call servicestack based web api from swift?

  8. 8

    Extracting values from a nested JSON file

  9. 9

    Retrieving values from an API call | Wix

  10. 10

    How do i call the parsed data from the GET request that have a longer nested JSON structure in Swift?

  11. 11

    Return specific values from array of hashes - JSON

  12. 12

    How to return nested class pointer from method?

  13. 13

    how to access nested Json key values in Golang

  14. 14

    How to search into nested JSON by values with Python

  15. 15

    How to store the values of a JSON response to an API Call in as many variables as the (unknown) number of objects using node.js

  16. 16

    How to call an ASP.NET Core Web API endpoint from within separate Docker Containers

  17. 17

    How to return a 304 result from ASP.NET Core web api?

  18. 18

    How to get the return values from a function?

  19. 19

    AutoMapper - how to return values from foreign key?

  20. 20

    How to return values from a threaded watchservice

  21. 21

    How to print all results of a JSON API call

  22. 22

    How to create json object dynamically from UI using values from input fields before sending to API

  23. 23

    How to populate jtable checkbox values from database?

  24. 24

    Converting nested dict from json into dataframe with values as columns

  25. 25

    Return "raw" json in ASP.NET Core 2.0 Web Api

  26. 26

    Web API does not serialize bool return type as a json boolean value

  27. 27

    Support both JSON and XML as return type in Web API 2

  28. 28

    Angular 2 & .NET Core Web API HttpPost の問題

  29. 29

    How to Return SQL FIeld Names in Javascript call to API?

ホットタグ

アーカイブ