skip to Main Content

I have the following JSON object and I have been trying to convert it to a datatype, say MessageData in C#, but I keep getting this error:

RuntimeBinderException: The best overloaded method match for ‘Newtonsoft.Json.JsonConvert.DeserializeObject<InstgramApiPro.Models.Root>(string)’ has some invalid arguments

The JSON is as follows:

{
  "username": "instagram",
  "user_id": "222",
  "API": "Backup",
  "story_by_id": "null",
  "stories": [
    {
      "media": "...",
      "Type": "Story-Video",
      "thumbnail": "..."
    },
    {
      "media": "...",
      "Type": "Story-Video",
      "thumbnail": "..."
    }
  ],
  "avatar": "will be added soon"
}

MessageResponse looks like this:

public class Root
{
    public string username { get; set; }
    public string user_id { get; set; }
    public string API { get; set; }
    public string story_by_id { get; set; }
    public List<Story> stories { get; set; }
    public string avatar { get; set; }
}

public class Story
{
    public string media { get; set; }
    public string Type { get; set; }
    public string thumbnail { get; set; }
}

This is how I am trying to deserialize it:

var body = await response.Content.ReadAsStringAsync();
var json = JsonConvert.DeserializeObject<dynamic>(body);
var myDeserializedClass = JsonConvert.DeserializeObject<Root>(json);

Please I need help on how to successfully convert this into messageResponse data.

I’m running it on ASP.NET Core 6

Thank you

2

Answers


  1. Just do:

    var body = await response.Content.ReadAsStringAsync();
    var myDeserializedClass = JsonConvert.DeserializeObject<Root>(body);
    
    Login or Signup to reply.
  2. var body = await response.Content.ReadAsStringAsync();
    var myDeserializedClass = JsonConvert.DeserializeObject<Root>(body);
    

    This will populate the myDeserializedClass object with the data from the JSON, following the structure defined in your Root and Story classes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search