发送 get 请求时不带括号返回

Posted

技术标签:

【中文标题】发送 get 请求时不带括号返回【英文标题】:return without brackets when sending a get request 【发布时间】:2021-11-30 05:31:32 【问题描述】:

我的 api 返回以下 json:


    "requestInfo": [
        
            "requestId": 118170,
            "firstRead": "1905-06-24T00:00:00",
            "requestAnswerSent": "2021-09-09T00:00:00",
            "currentMilage": "13131",
            "damageOccured": "1753-01-01T00:00:00",
            "milageDamageOccured": 0,
            "latestService": "1753-01-01T00:00:00",
            "latestServiceKm": 0,
            "contractID": 00000,
            "requestRecieved": "2021-10-08T16:18:58.237",
            "requestStatus": "NEW_REQUEST",
            "claiminfo": [
                
                    "claimId": 387498,
                    "name": "Claim",
                    "answerSent": "2021-09-09T00:00:00"
                ,
                
                    "claimId": 387499,
                    "name": "Rent Replacement Car",
                    "answerSent": "2021-09-09T00:00:00"
                ,
                
                    "claimId": 387500,
                    "name": "Other Work",
                    "answerSent": "2021-09-09T00:00:00"
                
            ]
        
    ]

而且我想返回它而不用括号括起来。我不能使用任何FOR JSON WITHOUT WRAPPER,因为我返回了一个列表,正如您在"claiminfo" 中看到的那样,但我仍然想返回而不将它包裹在 中。 再次由于返回列表,我也不能使用SingleOrDefault 之类的东西。这是代码(方法、模型和控制器)

public class RequestInfo
    
        //public int claimId  get; set;  = 0;
        public int requestId  get; set;  = 0;
        public DateTime firstRead  get; set;  = DateTime.Parse("1753-01-01");
        public DateTime requestAnswerSent  get; set;  = DateTime.Parse("2021-09-09");
        public string currentMilage  get; set;  = "";
        public DateTime damageOccured  get; set;  = DateTime.Parse("1753-01-01");
        public int milageDamageOccured  get; set;  = 0;
        public DateTime latestService  get; set;  = DateTime.Parse("1753-01-01");
        public int latestServiceKm  get; set;  = 0;
        public int contractID  get; set;  = 0;
        public DateTime requestRecieved  get; set;  = DateTime.Parse("1753-01-01");
        public string requestStatus  get; set;  = "";
        public IEnumerable<ClaimInfo> claiminfo  get; set; 
    

    public class ClaimInfo
    
        public int claimId  get; set;  = 0;
        public string name  get; set;  = "";
        public DateTime answerSent  get; set;  = DateTime.Parse("2021-09-09");
    

    public class RequestInformation
    
        public IEnumerable<RequestInfo> requestInfo  get; set; 
    

这是控制器:

[HttpGet]
        public async Task<IActionResult> getRequestInfo([FromQuery] RequestInfo model)
        

        if (!ModelState.IsValid)
        
            return BadRequest(ModelState);
        
        try
        
            var list = await _request.getRequestInfo(model);
            return Ok(list);
        
        catch (Exception ex)
        
            return BadRequest(ex.Message);
        
        return Ok();
    

最后是方法:

// request info card 
public async Task<RequestInformation> getRequestInfo(RequestInfo model)

    var parameters = new DynamicParameters();
    parameters.Add("@requestId", model.requestId);
    var getAllRequest = await _sqlconnection.QueryAsync<RequestInfo>($@"SELECT TOP 1
    Request.ID as requestId, 
    FirstRead as firstRead,
    CurrentKilometerreading as currentMilage, 
    DamageDate as damageOccured, DamageKilometerreading as milageDamageOccured,
    LatestServiceDate as latestService, LatestServiceKilometer as latestServiceKm,
    ClaimHandlingStatus.Name as requestStatus, Request.CreatedDate as requestRecieved, 
    Contract.ID as contractID
    FROM Request 
    INNER JOIN RequestCrossClaim ON Request.ID = RequestCrossClaim.RequestID
    INNER JOIN Claim ON RequestCrossClaim.ClamID = Claim.ID
    INNER JOIN Contract ON Request.ContractID = Contract.ID
    INNER JOIN ClaimHandlingStatus ON Request.ClaimHandlingStatusID = 
    ClaimHandlingStatus.ID
    WHERE Request.ID = @requestId", parameters);


    var getClaimsForRequet = await _sqlconnection.QueryAsync<ClaimInfo>($@"
    SELECT RequestType.Name, AnswerSent, Claim.ID as claimId
    FROM Request
    INNER JOIN RequestCrossClaim ON Request.ID = RequestCrossClaim.RequestID
    INNER JOIN Claim ON RequestCrossClaim.ClamID = Claim.ID
    INNER JOIN RequestType ON RequestTypeID = RequestType.ID
    WHERE Request.ID = @requestId", parameters);

    var claims = getClaimsForRequet;
    var request = new RequestInformation()
    
        requestInfo = getAllRequest
    ;

    foreach (var r in request.requestInfo)
    
        r.claiminfo = claims.OrderBy(s => s.claimId);
    

    return request;

感谢您的帮助。

【问题讨论】:

您能添加所需的输出吗? 看起来您想从控制器返回 OK(list.requestInfo),这将返回从 List 开始的 JSON,而不是从包装器对象开始。 【参考方案1】:

在 getRequestInfo 中尝试返回 OK(list.requestInfo) 而不是 OK(list)。意思是,从结果(列表)中提取对象 requestInfo。

【讨论】:

谢谢你做得很好!

以上是关于发送 get 请求时不带括号返回的主要内容,如果未能解决你的问题,请参考以下文章

requests不带参数的get请求和带get参数请求

将自引用对象列表序列化为不带方括号的 JSON

GET 请求返回发送页面的 HTML

AngularJS:POST请求返回302后发送的GET请求用于登录

php如何通过get方法发送http请求,并且得到返回的参数

发送 AJAX 请求时不显示引导模式