Log in

View Full Version : System.text.json with .NET 8 and VB.


ProWo
19th August 2024, 20:45
I am working on a winforms app in vb.
It should have .NET 8 as target framework and therefore the JavaScriptDeserializer does not work anymore (only works up to 4.8 .net_framework).
The new deserializer built into .NET (System.Text.Json) doesn't work with the previous code and I don't want to use the Newtonsoft deserializer as it requires an additional package.
Do any of you have an idea how I can read the value of red_x from the attached json file?
I failed with JsonDocument, JsonArray, JsonNode.

https://forum.videohelp.com/attachments/81649-1724084324/json.zip

LoRd_MuldeR
21st August 2024, 01:37
Try something like this:
using System.Text.Json;

string JSON_DATA = File.ReadAllText("your_input_file.json");

JsonDocument document = JsonDocument.Parse(JSON_DATA);
JsonElement frames = document.RootElement.GetProperty("frames");
JsonElement side_data_list = frames[0].GetProperty("side_data_list");
string? red_x = side_data_list[0].GetProperty("red_x").GetString();

Console.WriteLine($"red_x: {red_x}");

ProWo
21st August 2024, 09:15
Try something like this:
using System.Text.Json;

string JSON_DATA = File.ReadAllText("your_input_file.json");

JsonDocument document = JsonDocument.Parse(JSON_DATA);
JsonElement frames = document.RootElement.GetProperty("frames");
JsonElement side_data_list = frames[0].GetProperty("side_data_list");
string? red_x = side_data_list[0].GetProperty("red_x").GetString();

Console.WriteLine($"red_x: {red_x}");
That was it!
Specifying the index was the solution. Thank you very much. :thanks: