Search code examples
jsonasp.net-mvc-5

Json(JToken.Parse(response)) returns empty array


I have used HttpClient to consume an API and in turns i'm getting the response as serialized string from response.Content.ReadAsStringAsync().Result as mentioned below,

"{\r\n  \"responseCode\": 400,\r\n  \"errors\": [\r\n    {\r\n      \"errorCode\": \"RPM400\",\r\n      \"errorMessage\": \"A program already exists.\"\r\n    }\r\n  ]\r\n}"

I want to deserialize this as

{
  "responseCode": 400,
  "errors": [
    {
      "errorCode": "RPM400",
      "errorMessage": "A program already exists."
    }
  ]
}

Then i have to send it to the UI, the return type in the ASP.Net MVC 5 ActionMethod is ActionResult.

I have tried using Json(JObject.Parse(response)) and Json(JToken.Parse(response)) but i am getting the empty response in the postman as below

[
    [
        []
    ],
    [
        [
            [
                [
                    []
                ],
                [
                    []
                ]
            ]
        ]
    ]
]

The same Json(JObject.Parse(response)) is working in another application which is ASP.Net Core 2. Could you provide some help on this?


Solution

  • I have used stackOverflow's marked answer, i had to use the using System.Web.Script.Serialization; for the Deserialization then it worked because my .Net Framework was 4.7.2