Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# json解析

作者:惨美歌   发布日期:2025-08-25   浏览:70

using System;
using Newtonsoft.Json;

public class Program
{
    public static void Main()
    {
        // 定义一个JSON字符串
        string jsonString = @"{
            'Name': 'John',
            'Age': 30,
            'IsEmployed': true
        }";

        // 将JSON字符串反序列化为对象
        Person person = JsonConvert.DeserializeObject<Person>(jsonString);

        // 输出解析后的对象属性
        Console.WriteLine("Name: " + person.Name);
        Console.WriteLine("Age: " + person.Age);
        Console.WriteLine("Is Employed: " + person.IsEmployed);
    }
}

// 定义一个类来映射JSON数据
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public bool IsEmployed { get; set; }
}

解释说明:

  1. 引入命名空间:使用 Newtonsoft.Json 命名空间,这是一个非常流行的第三方库,用于处理 JSON 数据。
  2. 定义 JSON 字符串:在 Main 方法中定义了一个 JSON 格式的字符串 jsonString
  3. 反序列化 JSON:使用 JsonConvert.DeserializeObject<T> 方法将 JSON 字符串转换为 C# 对象。这里的 <T> 是你定义的类类型,在这个例子中是 Person 类。
  4. 输出结果:通过访问对象的属性,输出解析后的 JSON 数据。

注意:

  • 需要安装 Newtonsoft.Json 包,可以通过 NuGet 包管理器安装:Install-Package Newtonsoft.Json

上一篇:c# hashtable

下一篇:c# contains

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

c#网络编程

c# rectangle

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站