{ "data": { "du": { "id": "YANG_YAXIONG", "name": "杨亚雄", "pwd": "FF594070518970F18C33C95EB9A1FF2C", "image": "/UserImage/YANG_YAXIONG.PNG", "group": "A", "gender": 0, "grade": 0, "birthday": "0001-01-01T00:00:00", "privilege": 0 }, "dtoken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiWUFOR19ZQVhJT05HIiwiZXhwIjoxNjg5MzQxMjM4LCJpc3MiOiJqd3RJc3N1ZXJmZCIsImF1ZCI6Imp3dEF1ZGllbmNlZmR1c2VyIn0.1nwADadJfAQeL7Nvztrbi7ufW5pA-H18KR5jfxHB-tU" } }反序列化du,dtoken
时间: 2023-07-21 22:11:16 浏览: 641
javascript 事件
要将 JSON 字符串反序列化为指定类型,你可以创建一个类或结构来表示 JSON 数据的结构,并使用 `JsonConvert.DeserializeObject<T>` 方法进行反序列化。以下是一个示例代码:
```csharp
using Newtonsoft.Json;
public class Data
{
public User Du { get; set; }
public string DToken { get; set; }
}
public class User
{
public string Id { get; set; }
public string Name { get; set; }
public string Pwd { get; set; }
public string Image { get; set; }
public string Group { get; set; }
public int Gender { get; set; }
public int Grade { get; set; }
public DateTime Birthday { get; set; }
public int Privilege { get; set; }
}
public class Program
{
public static void Main()
{
string json = "{\"data\":{\"du\":{\"id\":\"YANG_YAXIONG\",\"name\":\"杨亚雄\",\"pwd\":\"FF594070518970F18C33C95EB9A1FF2C\",\"image\":\"/UserImage/YANG_YAXIONG.PNG\",\"group\":\"A\",\"gender\":0,\"grade\":0,\"birthday\":\"0001-01-01T00:00:00\",\"privilege\":0},\"dtoken\":\"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiWUFOR19ZQVhJT05HIiwiZXhwIjoxNjg5MzQxMjM4LCJpc3MiOiJqd3RJc3N1ZXJmZCIsImF1ZCI6Imp3dEF1ZGllbmNlZmR1c2VyIn0.1nwADadJfAQeL7Nvztrbi7ufW5pA-H18KR5jfxHB-tU\"}}";
var result = JsonConvert.DeserializeObject<Data>(json);
Console.WriteLine(result.Du.Id);
Console.WriteLine(result.Du.Name);
Console.WriteLine(result.DToken);
}
}
```
在上述示例中,我们创建了两个类 `Data` 和 `User` 来表示 JSON 数据的结构。然后,我们使用 `JsonConvert.DeserializeObject<Data>` 方法将 JSON 字符串反序列化为 `Data` 类型的对象。
最后,我们可以通过访问反序列化后的对象的属性来获取对应的值,并将它们输出到控制台上。
输出结果将是:
```
YANG_YAXIONG
杨亚雄
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiWUFOR19ZQVhJT05HIiwiZXhwIjoxNjg5MzQxMjM4LCJpc3MiOiJqd3RJc3N1ZXJmZCIsImF1ZCI6Imp3dEF1ZGllbmNlZmR1c2VyIn0.1nwADadJfAQeL7Nvztrbi7ufW5pA-H18KR5jfxHB-tU
```
请确保在使用之前安装了 Newtonsoft.Json 包。你可以使用 NuGet 包管理器或在项目文件中手动添加对该包的引用。
阅读全文