csharp解析json数据
时间: 2024-01-31 12:03:18 浏览: 32
以下是C#解析JSON数据的示例代码:
```csharp
using System;
using Newtonsoft.Json;
namespace JsonDemo
{
class Program
{
static void Main(string[] args)
{
// JSON字符串
string json = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
// 反序列化JSON字符串为对象
Person person = JsonConvert.DeserializeObject<Person>(json);
// 访问对象属性
Console.WriteLine("Name: " + person.Name);
Console.WriteLine("Age: " + person.Age);
Console.WriteLine("City: " + person.City);
}
}
// 定义Person类
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string City { get; set; }
}
}
```
输出结果为:
```
Name: John
Age: 30
City: New York
```