Cannot Deserialize the Json Array (E.G. [1,2,3]) into Type ' ' Because Type Requires Json Object (E.G. {"Name":"Value"}) to Deserialize Correctly

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

Cannot deserialize the current JSON array (e.g. [1,2,3])

I think the problem you're having is that your JSON is a list of objects when it comes in and it doesnt directly relate to your root class.

var content would look something like this (i assume):

[
{
"id": 3636,
"is_default": true,
"name": "Unit",
"quantity": 1,
"stock": "100000.00",
"unit_cost": "0"
},
{
"id": 4592,
"is_default": false,
"name": "Bundle",
"quantity": 5,
"stock": "100000.00",
"unit_cost": "0"
}
]

Note: make use of http://jsonviewer.stack.hu/ to format your JSON.

So if you try the following it should work:

  public static List<RootObject> GetItems(string user, string key, Int32 tid, Int32 pid)
{
// Customize URL according to geo location parameters
var url = string.Format(uniqueItemUrl, user, key, tid, pid);

// Syncronious Consumption
var syncClient = new WebClient();

var content = syncClient.DownloadString(url);

return JsonConvert.DeserializeObject<List<RootObject>>(content);

}

You will need to then iterate if you don't wish to return a list of RootObject.


I went ahead and tested this in a Console app, worked fine.

Sample Image

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'portal.Models.SubProjectViewRecord'

Apparently looks like from the error that there are multiple items in the array that is why there is any array returned from the api reponse. you can use a List<T> for it and the code for it would be like :

List<Models.SubProjectViewRecord> oop = JsonConvert.DeserializeObject<List<Models.SubProjectViewRecord>>(objJson);

While your model would be below assuming that the json array elements are of json object with Id and Name members in it:

public class SubProjectViewRecord
{
public string Name { get; set; }
public int Id { get; set; }
}

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

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 into type because the type requires a JSON array

you have several bugs in your code.Replace List<clsRaceTable> , List<clsCircuit> , List<clsLocation> with clsRaceTable,clsCircuit and clsLocation. Your classes should be

public class Root
{
public clsMRData MRData { get; set; }
}
public class clsMRData
{
public string xmlns { get; set; }
public string series { get; set; }
public string url { get; set; }
public string limit { get; set; }
public string offset { get; set; }
public string total { get; set; }
public clsRaceTable RaceTable { get; set; }
}

public class clsRaceTable
{
public string season { get; set; }
public List<clsRace> Races { get; set; }
}

public class clsRace
{
public string season { get; set; }
public string round { get; set; }
public string url { get; set; }
public string raceName { get; set; }
public clsCircuit Circuit { get; set; }
public string date { get; set; }
public string time { get; set; }
}

public class clsCircuit
{
public string circuitId { get; set; }
public string url { get; set; }
public string circuitName { get; set; }
public clsLocation Location { get; set; }
}

public class clsLocation
{
public string lat { get; set; }
[JsonProperty(PropertyName = "long")]
public string lon { get; set; }
public string locality { get; set; }
public string country { get; set; }
}

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


Related Topics



Leave a reply



Submit