Cannot Deserialize the Current JSON Object (E.G. {"Name":"Value"}) into Type 'System.Collections.Generic.List'1

Error: Cannot deserialize the current JSON object (e.g. { name : value }) into type 'System.Collections.Generic.List`1

Your JSON response is array and you have declared as single object. You must declare instrumentxxx as List<Instrumentxxx>

Here is the working code Click here

public class EndofDay
{

public List<Instrumentxxx> instrumentxxx { get; set; }

}

public class Instrumentxxx
{
public string id { get; set; }
public double APrice { get; set; }
public double BPrice { get; set; }
public double CPrice { get; set; }
}

Cannot deserialize the current JSON objectinto type 'System.Collections.Generic.List because the type requires a JSON arrayto deserialize correctly

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);

Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. { name : value }) to deserialize correctly

Your json string is wrapped within square brackets ([]), hence it is interpreted as array instead of single RetrieveMultipleResponse object. Therefore, you need to deserialize it to type collection of RetrieveMultipleResponse, for example :

var objResponse1 = 
JsonConvert.DeserializeObject<List<RetrieveMultipleResponse>>(JsonStr);

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array

The API returns a List<T> not a single object so you need to update the Deserialize line:

return JsonConvert.DeserializeObject<List<CovidStats>>(response);

UPDATE:

For completeness, you will also need to update the return type of the method. Full code below:

public static async Task<List<CovidStats>> GetByCountryLiveStats(string country, DateTime startDate, DateTime endDate)
{
var url = $"https://api.covid19api.com/country/{country}/status/confirmed/live?from= {startDate}&to={endDate}";
var client = new HttpClient();
var response = await client.GetStringAsync(url);
return JsonConvert.DeserializeObject<List<CovidStats>>(response);
}

Cannot deserialize the current JSON object (e.g. { name : value }) into type requires a JSON array (e.g. [1,2,3]) to deserialize correctly

You have two options here:


1st Option, you are missing a wrapper object for data

public class Wrapper<T>
{
public T Data { get; set; }
}

and then use:

var table = JsonConvert.DeserializeObject<Wrapper<List<PartOne>>>(json).Data;

2nd Option, deserialize it first as JObject and access to data and then deserialize it to the List<PartOne>:

var jObj = JsonConvert.DeserializeObject(json) as JObject;
var jArr = jObj.GetValue("data");
var table = jArr.ToObject<List<PartOne>>();

Cannot deserialize the current JSON object (e.g. { name : value }) into type 'System.Collections.Generic.IList problem

The error occurs since that your json file only corresponds to a PrometheusJson object instead of the List<PrometheusJson>,so when I modify code to below and it works:

List<PrometheusJson> dataG = new List<PrometheusJson>();
using (var httpClient = new HttpClient())
{
using (var response = await httpClient
.GetAsync("http://localhost:9090/api/v1/query?query=wmi_logical_disk_free_bytes"))
{
string apiResponse = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<PrometheusJson>(apiResponse);
dataG.Add(data);
}
Console.WriteLine(dataG);
}

Converting JSON to Object fails - Cannot deserialize the current JSON object into System.Collections.Generic.List

First of all you have to figure out what your API returns.

Right now you're trying to parse a List of jsonToObj Arrays (List<jsonToObj[]>). You have to decide whether to use a jsonToObj[] or List<jsonToObj> or a simple jsonToObj which your API provides now:

var a = JsonConvert.DeserializeObject<jsonToObj>(richTextBox1.Text);

But this then throws:

JSON integer 918819437284 is too large or small for an Int32. Path 'messages[0].recipient', line 25, position 33."

So make sure you use a Long for that.

public class messages
{
public string id { get; set; }
public long recipient { get; set; }
}

Furthermore you can add inDND to your jsonToObj class if you need the info:

public class jsonToObj
{
...
public string[] inDND { get; set; }
...
}

Deserialized JSON



Related Topics



Leave a reply



Submit