using Newtonsoft.Json; string json = "{\"name\" : \"John\", \"age\" : 30, \"city\" : \"New York\"}"; JsonReader reader = new JsonTextReader(new StringReader(json)); while (reader.Read()) { if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "name") { reader.Read(); string name = (string)reader.Value; Console.WriteLine(name); } if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "age") { reader.Read(); int age = (int)reader.Value; Console.WriteLine(age); } if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "city") { reader.Read(); string city = (string)reader.Value; Console.WriteLine(city); } }
using Newtonsoft.Json; string json = "[1, 2, 3, 4, 5]"; JsonReader reader = new JsonTextReader(new StringReader(json)); while (reader.Read()) { if (reader.TokenType == JsonToken.Integer) { int num = (int)reader.Value; Console.WriteLine(num); } }This example reads the JSON array and retrieves its items (1, 2, 3, 4, 5) using the JsonReader class. Package library: The package library used in these examples is Newtonsoft.Json, which is a popular .NET package for working with JSON data in C#. It provides a rich set of classes and methods for reading, manipulating, and serializing JSON data. To use this library in your project, you can install the Newtonsoft.Json package using NuGet or by downloading it from the official website.