using System.Json; JsonObject person = new JsonObject(); person.Add("firstName", "John"); person.Add("lastName", "Doe"); person.Add("age", 30); person.Add("address", new JsonObject() { { "street", "123 Main St" }, { "city", "New York" }, { "state", "NY" } }); string jsonText = person.ToString();
using System.Json; string jsonText = "{\"firstName\":\"John\",\"lastName\":\"Doe\",\"age\":30,\"address\":{\"street\":\"123 Main St\",\"city\":\"New York\",\"state\":\"NY\"}}"; JsonObject person = JsonObject.Parse(jsonText); string firstName = person["firstName"]; int age = person["age"];In this example, we start with some JSON data in a string format. We then use the static Parse() method of JsonObject to convert the string into a JsonObject. We can then access the properties of the object directly using the [] operator, just like a dictionary. Overall, JsonObject is an easy-to-use class that simplifies working with JSON data in C#. It is part of the System.Json library in the .NET Framework.