0

Tell me please. How can I get this data back in the JsonResult? I can't figure it out for 2 hours. My task: I need to take this data, return it to JsonResult and process it in ajax. I will be glad to any hint

public async Task<JsonResult> PostsPartial()
{
    var client = new RestClient($"https://jsonplaceholder.typicode.com/posts");
    var request = new RestRequest(Method.GET);
    IRestResponse response = await client.ExecuteAsync(request);
    var postsData = JsonConvert.DeserializeObject<JToken>(response.Content);

    return Json(postsData);
}
memento
  • 51
  • 5
  • Why do you want to return a JToken? What exactly is wrong with the code you show here? – DavidG Apr 28 '21 at 20:35
  • What version of asp.net-core are you using? asp.net-core 3.1 and later uses [tag:system.text.json] not [tag:json.net] as its JSON serializer. So you can either 1) Switch to `System.Text.Json` for serialization. 2) Revert to Newtonsoft as shown in [Where did IMvcBuilder AddJsonOptions go in .Net Core 3.0?](https://stackoverflow.com/q/55666826/3744182). 3) Write a custom converter to translate from Newtonsoft to System.Text.Json as shown in [How can I serialize a Newtonsoft JToken to JSON using System.Text.Json?](https://stackoverflow.com/q/65386083/3744182). – dbc Apr 28 '21 at 21:10
  • Try to set break point in the PostsPartial method, then step by step to debug your code, check whether the PostsPartial is executed or not, and whether the response data is correct or not? Besides trying to use F12 developer console tool to check if there has any error. – Zhi Lv Apr 29 '21 at 06:37

1 Answers1

0

try to use

   await New JsonResult(JObject.FromObject(postsData))
Alex
  • 1
  • 1