I have JSON data like shown below. I only included column names once to save data size. Please let me know how do I convert this to C# object list.
// JSON
{
"COLUMNS": [
"id", "country","lastname", "firstname", "email", "category"
],
"DATA": [
[43, "USA", "jon", "doe", "doe@gmail.com", "HR"],
[44, "JAPAN", "tanaka", "yidi", "yidi@aol.com", "IT"]
]
}
// .NET
Employee emp = JsonConvert.DeserializeObject<Employee>(jsonData);
I should be able to access object properties like emp.lastname.
Also, as the title indicates, I am using the JsonConvert class in the Json.NET library.
You won’t be able to use the JsonConvert class directly, as it expects all properties to have a name/mapping, and isn’t able to ascertain it because you are essentially using a custom format.
What you need to do is create a class that has the columns and the data and serialize into that, like so:
JsonConvert should be able to convert your data into this structure in .NET.
From there, it becomes more of a .NET issue, in that you have to get the names of the columns and data. You should be able to use LINQ to help with pairing the two and then use reflection to populate your item:
The only caveat for the above; if you are using a value type, then you will not populate the same instance as you iterate through this. In order to handle value types, you have to unwind the LINQ query and iterate through each row and column using for loops, creating the new instance on each new row and then using Reflection as above to populate the properties.