parse '["value1","value2"]' to a list in csharp

To parse the JSON string '["value1","value2"]' into a list in C#, you can make use of the Newtonsoft.Json library. You will first need to install the NuGet package for Newtonsoft.Json.

Here is an example code snippet that demonstrates how to parse the JSON string into a list:

main.cs
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        string jsonString = @"[""value1"",""value2""]";

        List<string> parsedList = JsonConvert.DeserializeObject<List<string>>(jsonString);

        Console.WriteLine("Parsed list:");
        foreach (string value in parsedList)
        {
            Console.WriteLine(value);
        }
    }
}
434 chars
20 lines

In this code snippet, we use the JsonConvert.DeserializeObject method from the Newtonsoft.Json library to deserialize the JSON string into a List of strings. The jsonString variable holds the input JSON string.

After the deserialization, we iterate over the parsed list and print its values.

Output:

main.cs
Parsed list:
value1
value2
27 chars
4 lines

Make sure you have the Newtonsoft.Json library installed, or you can install it via the NuGet package manager.

related categories

gistlibby LogSnag