parse json from string in csharp

To parse a JSON string in C#, you can use the Json.NET library which can be installed through NuGet. Here's an example of how to deserialize a JSON string into a C# object:

main.cs
using Newtonsoft.Json;

// JSON string to deserialize
string jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";

// Define a class for the JSON object
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string City { get; set; }
}

// Deserialize the JSON string into a Person object
Person person = JsonConvert.DeserializeObject<Person>(jsonString);

// Access the properties of the Person object
Console.WriteLine("Name: " + person.Name);
Console.WriteLine("Age: " + person.Age);
Console.WriteLine("City: " + person.City);
594 chars
21 lines

In this example, we're deserializing a JSON string that represents a person with a name, age, and city. We first define a class for the JSON object, and then use the JsonConvert.DeserializeObject<T> method to convert the JSON string into a Person object. We can then access the properties of the Person object to work with the deserialized data.

gistlibby LogSnag