C# 读取 JSON 数据 - 使用 Newtonsoft.Json 库
{/'title/':/'C# 读取 JSON 数据 - 使用 Newtonsoft.Json 库/',/'description/':/'使用 Newtonsoft.Json 库,你可以轻松地读取 C# 中的 JSON 数据。本文提供了示例代码,演示如何解析 JSON 字符串并提取信息。/',/'keywords/':/'C#, JSON, Newtonsoft.Json, 读取, 解析, 数据, 示例代码, JObject, NuGet, 安装/',/'content/':/'///'使用 Newtonsoft.Json 库,您可以轻松地读取 C# 中的 JSON 数据。///'//n///'本文提供了一个示例代码,演示如何解析 JSON 字符串并提取信息。///'//n//n///'csharp//nusing System;//nusing Newtonsoft.Json.Linq;//n//nclass Program//n{//n static void Main(string[] args)//n {//n string json = @/'{//n 'name': 'John',//n 'age': 30,//n 'city': 'New York'//n }/';//n//n // 解析 JSON 字符串//n JObject obj = JObject.Parse(json);//n//n // 读取信息//n string name = (string)obj[/'name/'];//n int age = (int)obj[/'age/'];//n string city = (string)obj[/'city/'];//n//n // 输出信息//n Console.WriteLine(/'Name: /' + name);//n Console.WriteLine(/'Age: /' + age);//n Console.WriteLine(/'City: /' + city);//n }//n}//n//n//n///'这段代码首先定义了一个 JSON 字符串,然后使用 JObject.Parse 方法将其解析为 JObject 对象。///'//n///'然后,您可以使用索引器来读取 JSON 中的信息。///'//n///'在示例代码中,我们读取了 ///'name///'、///'age///' 和 ///'city///' 字段的值,并将其打印到控制台上。///'//n//n///'请注意,您需要在项目中安装 Newtonsoft.Json 库,可以通过 NuGet 包管理器来安装。///'//n///'在 Visual Studio 中,右键单击项目,选择 ///'管理 NuGet 程序包///',然后搜索并安装 ///'Newtonsoft.Json///'。///'//n/
原文地址: https://www.cveoy.top/t/topic/pLUf 著作权归作者所有。请勿转载和采集!