简体   繁体   English

Returning object as JSON data using Web API C#

[英]Returning object as JSON data using Web API C#

I want to generate JSON data from a database table using Web API C#.我想使用 Web API ZD7EFA19FBE7D2972FD5ADB6024 从数据库表生成 JSON 数据The following is the table structure以下是表结构

CREATE TABLE [dbo].[Fields](
    [fieldID] [varchar](250) NOT NULL,
    [displayName] [varchar](500) NOT NULL,
    [id] [bigint] NOT NULL,
    [tenant] [bigint] NOT NULL,
    [Name] [varchar](500) NOT NULL,
    [description] [varchar](500) NULL,
    [type] [varchar](250) NULL
) ON [PRIMARY]
GO

And having following data并拥有以下数据

INSERT [dbo].[Fields] ([fieldID], [displayName], [id], [tenant], [Name], [description], [type]) VALUES (N'100', N'Loan#', 18, 3, N'Loan#', N'Loan#', N'string')
GO
INSERT [dbo].[Fields] ([fieldID], [displayName], [id], [tenant], [Name], [description], [type]) VALUES (N'101', N'LoanProgram', 19, 3, N'LoanProgram', N'LoanProgram', N'string')
GO

From this table and I want to generate a JSON in the following format using the Web API从此表中,我想使用 Web API 以下列格式生成 JSON

{
  "100": {
        "fieldID": "100",
        "displayName": "Loan#",
        "id": 18,
        "tenant": 3,
        "name": "Loan#",
        "description": "Loan#",
        "type": "string"        
    },
    "101": {
        "fieldID": "101",
        "displayName": "LoanProgram",
        "id": 19,
        "tenant": 3,
        "name": "LoanProgram",
        "description": "LoanProgram",
        "type": "string"        
    }
}

Following is my API controller以下是我的 API controller

[HttpGet("Fields/{id}/fields/")]
public Object GetFields(int id)
{
   return _fieldService.GetFields(id).Result;
}

I have created a class as follows我创建了一个 class 如下

public class ConfiguredFields
{
    public int fieldID { get; set; }
    public string displayName { get; set; }
    public int id { get; set; }
    public string tenant { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public string type { get; set; }        
}

And using Dapper I have called the SP and tried to the value并使用 Dapper 我调用了 SP 并尝试了该值

public async Task<Object> GetWorkflowFields(int WID)
{
    using (var db = new SqlConnection(_connectionString.Value))
    {
        var parameters = new DynamicParameters();
        parameters.Add("@pm_ID", WID);
        var result = await db.QueryAsync<ConfiguredFields>("SP_GetLoanFields", parameters, commandType: CommandType.StoredProcedure);

        
        return result.ToList();

    }
}

But I am getting the JSON in the following format (with array lap and not in the desired format where fieldID wise wrapping is not there.)但是我得到了以下格式的 JSON(使用数组圈,而不是不存在 fieldID 明智包装的所需格式。)

[
    {
        "fieldID": 100,
        "displayName": "Loan#",
        "id": 18,
        "tenant": "3",
        "name": "Loan#",
        "description": "Loan#",
        "type": "string"        
    },
    {
        "fieldID": 101,
        "displayName": "LoanProgram",
        "id": 19,
        "tenant": "3",
        "name": "LoanProgram",
        "description": "LoanProgram",
        "type": "string"   
    }
    
]

Please suggest what all changes required here to get the JSON in desired format?请建议在此处获得所需格式的 JSON 所需的所有更改? Please help me to solve this?请帮我解决这个问题?

  1. Change the return type of your GetWorkflowFields method from Task<Object> to Task<List<ConfiguredFields>> :GetWorkflowFields方法的返回类型从Task<Object>更改为Task<List<ConfiguredFields>>

     public async Task<List<ConfiguredFields>> GetWorkflowFields(int WID) {... }
  2. Change your model properties to have types that match the data in your database (based on the table definition you gave in your question).将您的 model 属性更改为具有与数据库中的数据匹配的类型(基于您在问题中提供的表定义)。

    Change fieldID from int to string . fieldIDint更改为string
    Change id from int to long .idint更改为long
    Change tenant from string to long .tenantstring更改为long

     public class ConfiguredFields { public string fieldID { get; set; } public string displayName { get; set; } public long id { get; set; } public long tenant { get; set; } public string name { get; set; } public string description { get; set; } public string type { get; set; } }
  3. Change your GetFields controller method to convert the result from GetWorkflowFields to a dictionary:更改您的GetFields controller 方法以将结果从GetWorkflowFields转换为字典:

     return _fieldService.GetWorkflowFields(id).Result.ToDictionary(f => f.fieldID);
  4. (Optional, but recommended) Change the return type of the GetFields controller method to Dictionary<string, ConfiguredFields> . (可选,但推荐)将GetFields controller 方法的返回类型更改为Dictionary<string, ConfiguredFields> Better yet, make the method async :更好的是,使方法async

     [HttpGet("Fields/{id}/fields/")] public async Task<Dictionary<string, ConfiguredFields>> GetFields(int id) { var fields = await _fieldService.GetWorkflowFields(id); return fields.ToDictionary(f => f.fieldID); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM