Home > Mobile >  Cannot deserialize the current JSON objectinto type 'System.Collections.Generic.List because th
Cannot deserialize the current JSON objectinto type 'System.Collections.Generic.List because th

Time:10-12

I'm playing around with the Imgur API and having trouble deseralizing the JSON response. My JSON looks like this:

{
  "data":{
    "id":"zPBszkd",
    "title":null,
    "description":null,
    "datetime":1634033771,
    "type":"image\/png",
    "animated":false,
    "width":391,
    "height":149,
    "size":11093,
    "views":0,
    "bandwidth":0,
    "vote":null,
    "favorite":false,
    "nsfw":null,
    "section":null,
    "account_url":null,
    "account_id":0,
    "is_ad":false,
    "in_most_viral":false,
    "has_sound":false,
    "tags":[
      
    ],
    "ad_type":0,
    "ad_url":"",
    "edited":"0",
    "in_gallery":false,
    "deletehash":"EUqn23MqOTdDOov",
    "name":"",
    "link":"https:\/\/i.imgur.com\/zPBszkd.png"
  },
  "success":true,
  "status":200
}

But I'm unsure how to deseralize this using Newtonsoft. I've read a couple of guides and questions but still keep running into the same error of:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[System.String]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

Would appreciate a code snippet that deseralizes this JSON correctly and an explanation of why it has to be done a certain way

CodePudding user response:

for converting any json , I would recommend using this website : https://json2csharp.com/ just copy your json and it creates a c# class based on it

for solving your issue, first create a model class :

    public class Data 
{
    public string id { get; set; }
    public object title { get; set; }
    public object description { get; set; }
    public int datetime { get; set; }
    public string type { get; set; }
    public bool animated { get; set; }
    public int width { get; set; }
    public int height { get; set; }
    public int size { get; set; }
    public int views { get; set; }
    public int bandwidth { get; set; }
    public object vote { get; set; }
    public bool favorite { get; set; }
    public object nsfw { get; set; }
    public object section { get; set; }
    public object account_url { get; set; }
    public int account_id { get; set; }
    public bool is_ad { get; set; }
    public bool in_most_viral { get; set; }
    public bool has_sound { get; set; }
    public List<object> tags { get; set; }
    public int ad_type { get; set; }
    public string ad_url { get; set; }
    public string edited { get; set; }
    public bool in_gallery { get; set; }
    public string deletehash { get; set; }
    public string name { get; set; }
    public string link { get; set; }
}

public class Root
{
    public Data data { get; set; }
    public bool success { get; set; }
    public int status { get; set; }
}

this is your escaped json converted to string:

var str="{\"data\":{\"id\":\"zPBszkd\",\"title\":null,\"description\":null,\"datetime\":1634033771,\"type\":\"image\\\/png\",\"animated\":false,\"width\":391,\"height\":149,\"size\":11093,\"views\":0,\"bandwidth\":0,\"vote\":null,\"favorite\":false,\"nsfw\":null,\"section\":null,\"account_url\":null,\"account_id\":0,\"is_ad\":false,\"in_most_viral\":false,\"has_sound\":false,\"tags\":[],\"ad_type\":0,\"ad_url\":\"\",\"edited\":\"0\",\"in_gallery\":false,\"deletehash\":\"EUqn23MqOTdDOov\",\"name\":\"\",\"link\":\"https:\\\/\\\/i.imgur.com\\\/zPBszkd.png\"},\"success\":true,\"status\":200}\r\n"

now you can deserialize it using Newtonsoft :

JsonConvert.DeserializeObject<Root>(str);

CodePudding user response:

If you are unsure about the correct class structure to deserialize a given JSON string into, you can make use of tools such as Json2CSharp or Visual Studios Paste Special feature which will generate the class structure for you:

public class Data
{
    public string id { get; set; }
    public object title { get; set; }
    public object description { get; set; }
    public int datetime { get; set; }
    public string type { get; set; }
    public bool animated { get; set; }
    public int width { get; set; }
    public int height { get; set; }
    public int size { get; set; }
    public int views { get; set; }
    public int bandwidth { get; set; }
    public object vote { get; set; }
    public bool favorite { get; set; }
    public object nsfw { get; set; }
    public object section { get; set; }
    public object account_url { get; set; }
    public int account_id { get; set; }
    public bool is_ad { get; set; }
    public bool in_most_viral { get; set; }
    public bool has_sound { get; set; }
    public List<object> tags { get; set; }
    public int ad_type { get; set; }
    public string ad_url { get; set; }
    public string edited { get; set; }
    public bool in_gallery { get; set; }
    public string deletehash { get; set; }
    public string name { get; set; }
    public string link { get; set; }
}

public class Root
{
    public Data data { get; set; }
    public bool success { get; set; }
    public int status { get; set; }
}

With the correct class structure given you can now deserialize the JSON string into an object of your model:

var result = JsonConvert.DeserializeObject<Root>(json);
  • Related