0

It is needed to parse JSONString into List. (List of instances) I'm trying to use JSON.NET by Newtonsoft.

I have classes:

    public class Item
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Url { get; set; }
        public string Manufactorers { get; set; }

    }

The JSON string looks something like this:

[
{     
 "Column0":23.0,
 "Column1":"Евроен",
 "Column2":"https://www.123.com",
 "Column3":"Фак"
},
{
 "Column0":24.0,
 "Column1":"Еил",
 "Column2":"https://www.123.com",
 "Column3":"Старт"
}
]

I've been trying to do something like this:

string JSONString = string.Empty;
JSONString = JsonConvert.SerializeObject(result);
List<Item> items = JsonConvert.DeserializeObject<List<Item>>(JSONString);

But it returns 0 and null. I have no idea, how to fix it.

Also here I truy to parse Excel file. This code works, but after deserialization, I have just 0 and null.

var filePath = @"..\..\..\..\doc.xlsx";

using (var steam = File.Open(filePath, FileMode.Open, FileAccess.Read))
{
   using (var reader = ExcelReaderFactory.CreateReader(steam))
   {
       var result = reader.AsDataSet().Tables["Лист1"];

       string JSONString = string.Empty;
       JSONString = JsonConvert.SerializeObject(result);

       List<Item> items = JsonConvert.DeserializeObject<List<Item>>(JSONString);

    }
}
7
  • 2
    This is not C, is it perhaps C#? Please pay attention when selecting tags. Commented Feb 24, 2020 at 9:30
  • 1
    There are no properties named Column0, Column1... in your class. That's why the Deserialization fails Commented Feb 24, 2020 at 9:34
  • 1
    In the shown code, what is result ? Commented Feb 24, 2020 at 9:36
  • If I add properties named Column0, Column1, I can see this error Newtonsoft.Json.JsonReaderException: 'Could not convert string to integer: ID. Path '[0].Column0', line 1, position 16.' Commented Feb 24, 2020 at 9:36
  • result is var result = reader.AsDataSet().Tables["Лист1"];.This is part of the parsing Excel file. Commented Feb 24, 2020 at 9:38

1 Answer 1

1

The naming of JSON and your class does not match. This can be fixed using JsonProperty attributes:

[JsonProperty("Column0")]
public decimal ID { get; set; }

Second, JSON deserizlizer can not deserialize string "23.0" to int when there is decimal point. You can retype ID to decimal or double to make it work.

Little test here:

public class TestClass
{
    [JsonProperty("Column0")]
    public decimal ID { get; set; }
}

Then the deserialization works without errors:

var testClassJson = "{\"Column0\": 12.0}";
var i = JsonConvert.DeserializeObject<TestClass>(testClassJson);
Sign up to request clarification or add additional context in comments.

1 Comment

It works, but with type string [JsonProperty("Column0")] public string ID { get; set; }

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.