using System;
using Newtonsoft.Json.Linq;
class Program
{
static void Main()
{
// 示例 JSON 字符串
string jsonString = @"{
'name': '张三',
'age': 25,
'isStudent': true,
'courses': ['数学', '物理', '化学']
}";
// 解析 JSON 字符串为 JObject 对象
JObject jsonObject = JObject.Parse(jsonString);
// 访问 JSON 数据中的字段
string name = (string)jsonObject["name"];
int age = (int)jsonObject["age"];
bool isStudent = (bool)jsonObject["isStudent"];
JArray courses = (JArray)jsonObject["courses"];
// 输出解析后的数据
Console.WriteLine($"姓名: {name}");
Console.WriteLine($"年龄: {age}");
Console.WriteLine($"是否是学生: {isStudent}");
Console.WriteLine("课程:");
foreach (var course in courses)
{
Console.WriteLine($"- {course}");
}
}
}
Newtonsoft.Json.Linq 命名空间,它提供了处理 JSON 数据的类。JObject.Parse() 方法将 JSON 字符串解析为 JObject 对象。string、int、bool 等)。上一篇:c#字符串转int
下一篇:c# web开发
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站