0

I post a api to get data, the response like json format, but the Content-Type was text/html

This is my code:

public ActionResult Test(string id)
{
    var request = GetMyRequest(id);
    RestClient client = new RestClient("baseUrl");
    var result = client.Execute(request).Content;
    var result2 = JObject.Parse(result);
    return Json(result2, JsonRequestBehavior.AllowGet);
}

I got the result

enter image description here

How can i deserialize the response correctly

Max
  • 4,439
  • 2
  • 18
  • 32
  • 1
    It would be helpful if you add the content of `result` before deserialization. – Zephyr Aug 07 '17 at 02:51
  • Are you getting only square brackets in the JSON value? – Chetan Aug 07 '17 at 03:17
  • 1
    Which version of asp.net-mvc are you using? Up to 4.0 still uses `JavaScriptSerializer` which will not know how to serialize a `JObject`. To switch to Json.NET see [Setting the Default JSON Serializer in ASP.NET MVC](https://stackoverflow.com/q/14591750) or [Using JSON.NET as the default JSON serializer in ASP.NET MVC 3 - is it possible?](https://stackoverflow.com/q/7109967). Or try [Exception converting JSON.NET JObject to JsonResult](https://stackoverflow.com/a/5572645) for a single method. – dbc Aug 07 '17 at 03:17
  • @dbc I use ASP.NET MVC 5 – Max Aug 07 '17 at 03:40
  • @ChetanRanpariya if I create a model(class) to deserialize, it work correctly(not noly square brackets) – Max Aug 07 '17 at 03:42
  • In that case I think we'll need more information, ideally an [mcve]. What is the content of the string `result`? – dbc Aug 07 '17 at 03:43
  • @Gary - I'm honestly not sure if MVC5 is using Json.NET by default or not. You might try doing return `Content(JsonConvert.SerializeObject(result2), "application/json");` as shown [here](https://stackoverflow.com/a/19876131) to see if it fixes the problem. Or wait -- since result is **already** a string try `return Content(result, "application/json");` – dbc Aug 07 '17 at 03:54

0 Answers0