parse json in csharp

To parse JSON in C#, you can use the Newtonsoft.Json package, also known as Newtonsoft JSON:

main.cs
using Newtonsoft.Json;

class Program
{
    static void Main(string[] args)
    {
        string json = @"{
            'name': 'John Smith',
            'age': 30,
            'city': 'New York'
        }";

        dynamic data = JsonConvert.DeserializeObject(json);

        Console.WriteLine("Name: " + data.name);
        Console.WriteLine("Age: " + data.age);
        Console.WriteLine("City: " + data.city);
    }
}
423 chars
20 lines

In this example, we parse a JSON string that contains information about a person's name, age, and city. The JsonConvert.DeserializeObject() method converts the JSON string to a dynamic object, which allows us to access its properties using dot notation.

related categories

gistlibby LogSnag