使用Newtonsoft直接读取Json格式文本(Linq to Json)

使用Newtonsoft直接读取Json格式文本(Linq to Json)

使用 Newtonsoft.Json(通常简称为 Newtonsoft)可以轻松地处理 JSON 格式的文本。Newtonsoft.Json 是 .NET 中一个流行的 JSON 处理库,它提供了丰富的功能和灵活性。

以下是使用 Newtonsoft.Json 进行 Linq to JSON 的示例代码:

首先,你需要在项目中安装 Newtonsoft.Json 包。你可以通过 NuGet 包管理器或者 .NET CLI 来安装该包。如果你使用 Visual Studio,可以右键点击项目,选择"管理 NuGet 程序包",然后搜索并安装 Newtonsoft.Json。

接下来,假设有一个 JSON 格式的文本如下:

json 复制代码
{
  "name": "John Doe",
  "age": 30,
  "email": "john.doe@example.com",
  "address": {
    "city": "New York",
    "zipCode": "10001"
  },
  "hobbies": [
    "reading",
    "swimming",
    "cooking"
  ]
}

使用 Newtonsoft.Json,你可以读取并解析这个 JSON 文本:

csharp 复制代码
using System;
using Newtonsoft.Json.Linq;

namespace JsonParsing
{
    class Program
    {
        static void Main()
        {
            // JSON 格式的文本
            string jsonText = @"{
                'name': 'John Doe',
                'age': 30,
                'email': 'john.doe@example.com',
                'address': {
                    'city': 'New York',
                    'zipCode': '10001'
                },
                'hobbies': [
                    'reading',
                    'swimming',
                    'cooking'
                ]
            }";

            // 解析 JSON 文本为 JObject
            JObject jsonObject = JObject.Parse(jsonText);

            // 获取具体属性值
            string name = (string)jsonObject["name"];
            int age = (int)jsonObject["age"];
            string email = (string)jsonObject["email"];

            JObject address = (JObject)jsonObject["address"];
            string city = (string)address["city"];
            string zipCode = (string)address["zipCode"];

            JArray hobbies = (JArray)jsonObject["hobbies"];

            Console.WriteLine("Name: " + name);
            Console.WriteLine("Age: " + age);
            Console.WriteLine("Email: " + email);
            Console.WriteLine("City: " + city);
            Console.WriteLine("Zip Code: " + zipCode);

            Console.WriteLine("Hobbies:");
            foreach (var hobby in hobbies)
            {
                Console.WriteLine("- " + (string)hobby);
            }
        }
    }
}

运行以上代码,你将得到输出:

json 复制代码
Name: John Doe
Age: 30
Email: john.doe@example.com
City: New York
Zip Code: 10001
Hobbies:
- reading
- swimming
- cooking

在这个示例中,我们使用 JObject.Parse 方法将 JSON 文本解析为 JObject,然后通过键值索引的方式获取其中的属性值。如果属性是对象或数组类型,我们可以继续使用 JObjectJArray 对象进行进一步的操作。

通过使用 Newtonsoft.Json,你可以灵活地读取和解析 JSON 格式的文本,并方便地提取所需的数据。它是 .NET 开发中处理 JSON 数据的强大工具。

相关推荐
上位机付工2 小时前
C#与倍福TwinCAT3进行ADS通信
开发语言·c#
土了个豆子的3 小时前
02.继承MonoBehaviour的单例模式基类
开发语言·visualstudio·单例模式·c#·里氏替换原则
疯狂的维修3 小时前
c#中public类比博图
c#·自动化
土了个豆子的6 小时前
03.缓存池
开发语言·前端·缓存·visualstudio·c#
CodeCraft Studio7 小时前
Excel处理控件Aspose.Cells教程:使用 Python 将 Pandas DataFrame 转换为 Excel
python·json·excel·pandas·csv·aspose·dataframe
何双新10 小时前
第 3 讲:KAFKA生产者(Producer)详解
分布式·kafka·linq
xiaowu0801 天前
策略模式-不同的鸭子的案例
开发语言·c#·策略模式
敬业小码哥1 天前
记一次:mysql的json及json数组使用组合使用
数据库·mysql·json
一键三联啊1 天前
BSON 和 JSON 的区别
json
VisionPowerful1 天前
九.弗洛伊德(Floyd)算法
算法·c#