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

C#asp.netJsonDeserialization

C# Problem Overview


I have a class like this:

public class MyStok
{
    public int STId { get; set; }
    public int SM { get; set; }
    public string CA { get; set; }
    public string Br { get; set; }
    public string BNo { get; set; }
    public decimal Vat { get; set; }
    public decimal Price { get; set; }
}

I deserialize like this:

string sc = e.ExtraParams["sc"].ToString();
MyStok myobj = JSON.Deserialize<MyStok>(sc);

My output seems to be like this (string sc) on fiddler:

[    {        "STId": 2,        "CA": "hbh",        "Br": "jhnj",        "SM": 20,        "Vat": 10,        "Price": 566,        "BNo": "1545545"    }]

But I get the error:

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

What is wrong in that code?

C# Solutions


Solution 1 - C#

It looks like the string contains an array with a single MyStok object in it. If you remove square brackets from both ends of the input, you should be able to deserialize the data as a single object:

MyStok myobj = JSON.Deserialize<MyStok>(sc.Substring(1, sc.Length-2));

You could also deserialize the array into a list of MyStok objects, and take the object at index zero.

var myobjList = JSON.Deserialize<List<MyStok>>(sc);
var myObj = myobjList[0];

Solution 2 - C#

For array type Please try this one.

 List<MyStok> myDeserializedObjList = (List<MyStok>)Newtonsoft.Json.JsonConvert.DeserializeObject(sc, typeof(List<MyStok>));

Please See here for details to deserialise Json

Solution 3 - C#

I ran into this exact same error message. I tried Aditi's example, and then I realized what the real issue was. (Because I had another apiEndpoint making a similar call that worked fine.) In this case The object in my list had not had an interface extracted from it yet. So because I apparently missed a step, when it went to do the bind to the

List<OfthisModelType>

It failed to deserialize.

If you see this issue, check to see if that could be the issue.

Solution 4 - C#

Another alternative if you have a root level array with no name and you actually want to use it for something, like adding methods

public class MyStoks : List<MyStok> 
{

}

Then you can deserialize like this

MyStoks myObj = JsonConvert.DeserializeObject<MyStoks>(jsonString);

Solution 5 - C#

HttpClient webClient = new HttpClient();
Uri uri = new Uri("your url");
HttpResponseMessage response = await webClient.GetAsync(uri)
var jsonString = await response.Content.ReadAsStringAsync();
var objData = JsonConvert.DeserializeObject<List<CategoryModel>>(jsonString);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsakirView Question on Stackoverflow
Solution 1 - C#Sergey KalinichenkoView Answer on Stackoverflow
Solution 2 - C#JantyView Answer on Stackoverflow
Solution 3 - C#VeretaxView Answer on Stackoverflow
Solution 4 - C#JohnView Answer on Stackoverflow
Solution 5 - C#Aditi_SystematixView Answer on Stackoverflow