Hi @Peter Bishop , Welcome to Microsoft Q&A,
You can use System.Text.Json
to simplify your code. Here is a .Net 8.o Console project:
using System.Text.Json;
string json = "{ \"data\": [{\"tag1\": 1,\"tag2\": 2,\"section1\": {\"tag3\": \"Dummy\"},\"tag4\": 3,\"array1\": [\"dummy\",\"dummy\"]}," +
"{\"tag1\": 4,\"tag2\": 5,\"section1\": {\"tag3\": \"Dummy\"},\"tag4\": 6,\"array1\": [\"dummy\",\"dummy\"]}," +
"{\"tag1\": 7,\"tag2\": 8,\"section1\": {\"tag3\": \"Dummy\"},\"tag4\": 9,\"array1\": [\"dummy\",\"dummy\"]}]}";
using JsonDocument doc = JsonDocument.Parse(json);
JsonElement root = doc.RootElement;
if (root.TryGetProperty("data", out JsonElement dataArray) && dataArray.ValueKind == JsonValueKind.Array)
{
List<string> addresses = new();
foreach (JsonElement element in dataArray.EnumerateArray())
{
addresses.Add(element.GetRawText());
}
for (int i = 0; i < addresses.Count; i++)
{
Console.WriteLine($"Entry{i + 1}: {addresses[i]}");
}
}
Console.ReadLine();
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.