2017-11-20 6 views
0

JSON을 간단한 텍스트 형식으로 반환하는 다음 작동 컨트롤러 메서드가 있습니다.ASP.NET 컨트롤러 메서드에서 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#에 익숙하지 않아서 JSON을 구성하기위한 다른 C# inbuilt 메서드를 알지 못합니다.

답변

3

실제로 JSON으로 응답을 사용한다고 가정하면이 문제를 해결할 수 있습니다. 다른 접근 방법을 원하는 경우

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

을 수행하여 당신은 다음과 같은 방법으로 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"} 

당신은 다음을 직렬화는 객체의 클래스 및 저장 값을 만들어야합니다. 목록이 있으면 해당 클래스의 List를 사용하여 직렬화 할 수 있습니다.

0

이러한 속성에 대한 래퍼 클래스를 만들어서 반환 할 수 있습니다.

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

또는 당신은 JsonResult

에 IActionResult에서 재곡 값을 변경할 수 있습니다