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}");
        }
    }
}
相关推荐
承渊政道22 分钟前
【Python编程—从入门到实践】(记分与闯关:用Python+Pygame,打造越战越精彩的太空大战!)
开发语言·python·pycharm·游戏程序·pygame
爱学习的程序媛23 分钟前
Python常用框架梳理
开发语言·后端·python·ai·web·后端框架·python框架
eggwyw2 小时前
Anaconda安装卸载的详细流程指南
开发语言·python
用户788477316345 小时前
WinForms 迁移 WPF:Upgrade Assistant 覆盖 30%,剩下 70% 我手写了六周
c#
another heaven6 小时前
【C/C++ Ranges 是零开销抽象吗?拆开管道、惰性求值和编译期膨胀】
c语言·开发语言·c++
SomeOtherTime8 小时前
Rust 闭包语法和原理(AI回答)
开发语言·后端·rust
apcipot_rain8 小时前
C++转Java日记(10)
java·开发语言
总要学点什么9 小时前
第105篇:三维特效-流动图片墙体效果——让箭头沿墙长滚,它俩竟是同一份代码
开发语言·javascript·ecmascript·gis·webgl·材质·cesium
xiaoye-duck9 小时前
《C++ 项目:手写大模型接入 SDK》会话模块整合:内存 SessionManager 与 DataManager 持久化联动
开发语言·数据库·c++·语言模型
寂寞的船10 小时前
【Unity知识速递】第13期 Unity 内存泄漏排查实战:从 Memory Profiler 到 Lua 引用泄漏的生产级全链路诊断
unity·junit·c#·游戏引擎·lua