C# 字符串转json

你可以使用Newtonsoft.Json(即Json.NET)库来解析JSON字符串

csharp 复制代码
using Newtonsoft.Json;
using System;
 
namespace JsonParsingExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string json = "{\"Name\":\"John\", \"Age\":30}";
            dynamic data = JsonConvert.DeserializeObject(json);
 
            Console.WriteLine($"Name: {data.Name}");
            Console.WriteLine($"Age: {data.Age}");
        }
    }
}

或者绑定类显示

csharp 复制代码
using Newtonsoft.Json;
using System;
 
namespace JsonParsingExample
{
    class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            string json = "{\"Name\":\"John\", \"Age\":30}";
            Person person = JsonConvert.DeserializeObject<Person>(json);
 
            Console.WriteLine($"Name: {person.Name}");
            Console.WriteLine($"Age: {person.Age}");
        }
    }
}
相关推荐
唐青枫14 小时前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫1 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
咕白m6252 天前
.NET 环境下 Word 超链接批量提取方案
c#·.net
用户91721561902112 天前
C# 通信协议增量解析:用状态机处理半包和粘包
c#
小码编匠2 天前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
疯狂SQL3 天前
手写高性能在线 JSON 工具|Web Worker 工程化打包 + 语法自动修复 + 多语言代码生成实战
typescript·json·next.js·web worker·前端性能优化·esbuild·源码实战
唐青枫4 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech5 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf6 天前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m6256 天前
使用 C# 在 Excel 中应用多种字体样式
后端·c#