繁体   English   中英

在ASP.NET控制器方法中返回JSON

[英]Returning JSON in ASP.NET controller method

我有以下工作控制器方法,该方法以简单文本格式返回JSON。

[HttpPost]
public IActionResult DecodeBarcode(string productCodeScheme, string productCode, string serialNumber, string batch, string expirationDate, int commandStatusCode) {
    string TextAreaResult = string.Empty;
    try {
        TextAreaResult = string.Format("{0} {1} {2}", request.getHttpInformation(), request.getHttpWarning(), request.getHttpResponseCode());
    } catch (Exception exc) {
        TextAreaResult = "Exception: " + exc.Message;
    }
    return Json(TextAreaResult);
}

运行上述方法后的输出看起来像

"The pack is active No warning 200"

request.getHttpInformation() is The pack is active
request.getHttpWarning() is No warning
request.getHttpResponseCode() is 200

现在,我尝试将响应分为3个不同的键值对,以便我的响应看起来像

{
  httpInformation: "The pack is active",
  httpWarning: "No Warning",
  httpResponseCode: "200"
}

如何在return Json(TextAreaResult)调用中传递其他参数?

如果我喜欢以下内容,它将无法正常工作

[HttpPost]
public IActionResult DecodeBarcode(string productCodeScheme, string productCode, string serialNumber, string batch, string expirationDate, int commandStatusCode) {
    string TextAreaResult = string.Empty;
    string TextAreaResultHttpInformation = string.Empty;
    string TextAreaResultHttpWarning = string.Empty;
    string TextAreaResultHttpResponseCode = string.Empty;

    try {
        TextAreaResultHttpInformation = string.Format("{0}}", request.getHttpInformation());

        TextAreaResultHttpWarning = string.Format("{1}}", request.getHttpWarning());

        TextAreaResultHttpResponseCode = string.Format("{2}}", request.getHttpResponseCode());
    } catch (Exception exc) {
        TextAreaResult = "Exception: " + exc.Message;
    }

    return Json(TextAreaResultHttpInformation, TextAreaResultHttpInformation, TextAreaResultHttpResponseCode);
}

如何构造键值对并以JSON形式返回? 也许, Json方法不是这里的正确选择,但是对于C#来说是新手,我不知道其他任何C#内置方法来构造JSON

假设您实际上想将响应作为JSON使用,则可以通过执行以下操作来实现

return Json(new 
{
    HttpInformation = TextAreaResultHttpInformation, 
    HttpWarning = TextAreaResultHttpWarning,
    StatusCode = TextAreaResultHttpResponseCode
});

您可以为这些属性创建包装器类并返回它。

public class BarcodeResultDto
    {
        [JsonProperty("httpInformation")]
        public string HttpInformation { get; set; }

        [JsonProperty("httpWarning")]
        public string HttpWarning { get; set; }

        [JsonProperty("httpResponseCode")]
        public int GetHttpResponseCode { get; set; }
    }

public IActionResult DecodeBarcode(string productCodeScheme, string productCode, string serialNumber,
            string batch, string expirationDate, int commandStatusCode)
        {
            var barcodeResult = new BarcodeResultDto
            {
                GetHttpResponseCode = 200,
                HttpInformation = "The pack is active",
                HttpWarning = "No Warning"
            };

            // your code here
            return Ok(barcodeResult);
        }

或者,您可以将重新调整后的值从IActionResult更改为JsonResult

如果您想使用其他方法,则可以通过以下方式使用JsonSerializer:

// Creating BlogSites object  
BlogSites bsObj = new BlogSites()  
{  
   Name = "test-name",  
   Description = "test-description"  
};  

// Serializing object to json data  
JavaScriptSerializer js = new JavaScriptSerializer();  
string jsonData = js.Serialize(bsObj); // {"Name":"test-name","Description":"test-description"}  

您只需要创建一个类并将值存储在其对象中,然后对其进行序列化即可。 如果有列表,则可以使用该类的列表,然后进行序列化。

暂无
暂无

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

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