8

I have this Json object:

{
   "Sheet1": [
      {
         "one": 1,
         "two": 18
      },
      {
         "one": 16,
         "two": 33
      },
      {
         "one": 17,
         "two": 34
      }
   ]
}

And I am trying to deserialize it using the following model:

public class Sheets
{
    [JsonProperty("Sheet1")]
    public Sheet Sheet { get; set; }
}

public class Sheet
{
    public List<Row> Rows { get; set; }
}

public class Row
{
    [JsonProperty("one")]
    public string Col1 { get; set; }

    [JsonProperty("two")]
    public string Col2 { get; set; }
}

var res = JsonConvert.DeserializeObject<Sheets>(result);

but I'm getting this exception:

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?

EDIT

One possible solution is to use

dynamic dynamicObject = JsonConvert.DeserializeObject(result);

but I want to deserialize it directly into my model.

4 Answers 4

3

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; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

No problem, be advised that i would change the property List<Row> Sheet to be more accurate, something like List<Row> Rows
2

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

3 Comments

Thanks for the answer Hakam, it looks good to me, but I'm not sure why I'm getting An unhandled exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll
@HoomanYar I updated the answer to make better names of the classes and properties. I could not figure out why the old code is producing that exception on your machine ,it was working correctly on mine without any exception and the result was correct.
after naming the classes better I know my answer is very like the accepted answer, but I just does not intended this
0

As error shown you should use a list instead of Sheet class. Try this:

     {"Sheets" :
       { 
       "Sheet1": [
          {
             "one": 1,
             "two": 18
          },
          {
             "one": 16,
             "two": 33
          },
          {
             "one": 17,
             "two": 34
          }
       ]
     }
    }

3 Comments

Well thanks but that's a different json object that what I have
You can change your class in this statement too: var res = JsonConvert.DeserializeObject<Sheet>(result);
that's actually the problem. It'll retrun null result using that code since Sheet is like an inner object to it
0

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

1 Comment

No problem, but then, Hakam Fostok's answer should do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.