You have read the raw JSON file into a string, now you need to deserialize it.
1. You need a class structure to represent the JSON data. For that I like to use:
JSON Utils: Generate C#, VB.Net, SQL TAble and Java from JSON[
^]
2. Next you need to use a JSON serialization library. If
.Net Framework 4.8 or earlier, use
NuGet Gallery | Newtonsoft.Json[
^], if Dot Net 3.1 or later, use
https://learn.microsoft.com/en-us/dotnet/api/system.text.json[
^]
Here are articles that I wrote for questions like this one:
*
Working with Newtonsoft.Json in C# & VB[
^]
*
Working with System.Text.Json in C#[
^]
UPDATE
Based on the raw JSON provided:
[
{
"Id": 5,
"GID": 1,
"SID": 34
},
{
"Id": 4,
"GID": 1,
"SID": 33
},
{
"Id": 3,
"GID": 1,
"SID": 32
}
]
You need to change your
EmployeeItem
class as follows:
public class EmployeeItem
{
[JsonProperty("Id")]
public int Id { get; set; }
[JsonProperty("GID")]
public long GroupID { get; set; }
[JsonProperty("SID")]
public long SiteID { get; set; }
}
The square brackets indicate an array of
EmployeeItem
objects, of which, with the reformatted JSON, we can see that there are 3 - IDs 5, 4, 3. We need to make sure that we deserialize a collection of objects.
The
JsonProperty
attribute maps the JSON property name to your
EmployeeItem
class property name. If the
EmployeeItem
class property name does not match or map, then no data will be deserialized.
Now you can Deserialize:
string rawJson = File.ReadAllText("file path of the text file");
List<EmployeeItem> employees = JsonConvert.DeserializeObject<List<EmployeeItem>>(rawJson);
I cover all of this in the articles listed above including useful tools to help you with the raw data.
Hope this helps!