你可以使用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}");
}
}
}