Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll
Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ExcelConsoleApp.Sheet' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
What am I doing wrong? Any thoughts?
One possible solution is to use
dynamic dynamicObject = JsonConvert.DeserializeObject(result);
but I want to deserialize it directly into my model.
Sheet1
is not a Sheet type but a List of Rows. This can be identify by brackets.
i.e "Sheet1": [
which is a clear sign for Collection and not an Object which is identified by {
.
Change Sheets to the following:
public class Sheets
[JsonProperty("Sheet1")]
public List<Row> Sheet { get; set; }
–
this is the model you need, I tested it and it was working exactly as you want.
and there is NO need to change the JSON structure.
public class SheetRoot
[JsonProperty("Sheet1")]
public List<Row> Sheet { get; set; }
public class Row
[JsonProperty("one")]
public int Col1 { get; set; }
[JsonProperty("two")]
public int Col2 { get; set; }
var res = JsonConvert.DeserializeObject<SheetRoot>(s);
–
–
–
–
–
Your structures are incompatible.
Inferring from the JSON, the example object can be traversed as Sheet1[i].one
or Sheet1[i].two
. i
being the index. Whereas, the C# model that you have posted will be traversed as SheetsObj.Sheet.Row[i].Col1
.
You could try changing your JSON or model.
So, maybe change your JSON to
"Sheet1" : {
"Rows": [
"one": 1,
"two": 18
"one": 16,
"two": 33
"one": 17,
"two": 34
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.