簡體   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