我不知道如何创建C#类来反序列化我的json

时间:2015-05-25 19:46:44

标签: c# json restsharp

我不知道如何使用RestSharp反序列化以下JSon:

[
{
    "id": "2",
    "username": "visiteur",
    "username_canonical": "visiteur",
    "email": "visiteur@localhost.fr",
    "email_canonical": "visiteur@localhost.fr",
    "enabled": "1",
    "salt": "031f1778d6926d472cd3959e12539709",
    "password": "KmWroMkLkW1/DNsWXJKcAMdGxYso2uDN3uMtkh2dlNo7jHROqrG8ZyEJl0Fj8YJ4Og3HXcx67SlqLBngFpWQmQ==",
    "last_login": "2015-05-20 14:09:37",
    "locked": "0",
    "expired": "0",
    "expires_at": null,
    "confirmation_token": null,
    "password_requested_at": null,
    "roles": "a:1:{i:0;s:13:\"ROLE_VISITEUR\";}",
    "credentials_expired": "0",
    "credentials_expire_at": null,
    "nom": "Leroy",
    "prenom": "Jean",
    "adresse": "127 rue Locale",
    "cp": "12700",
    "ville": "Host",
    "dateEmbauche": "2015-01-01"
}
]

其实我是以下课程:

using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace GSB_FicheFrais
{
    public class GetUserResult
    {
        public User Results { get; set; }
    }

    public class User
    {
        public int id { get; set; }
        public String username { get; set; }
    }

}

你能帮我创建那门课程吗? 我试图创建一个元素列表,只是一个用户元素,但没有人工作。

1 个答案:

答案 0 :(得分:1)

您的JSon的C#表示错误。
尝试使用JSon2CSharp。它为您提供以下模型:

public class RootObject
{
    public string id { get; set; }
    public string username { get; set; }
    public string username_canonical { get; set; }
    public string email { get; set; }
    public string email_canonical { get; set; }
    public string enabled { get; set; }
    public string salt { get; set; }
    public string password { get; set; }
    public string last_login { get; set; }
    public string locked { get; set; }
    public string expired { get; set; }
    public object expires_at { get; set; }
    public object confirmation_token { get; set; }
    public object password_requested_at { get; set; }
    public string roles { get; set; }
    public string credentials_expired { get; set; }
    public object credentials_expire_at { get; set; }
    public string nom { get; set; }
    public string prenom { get; set; }
    public string adresse { get; set; }
    public string cp { get; set; }
    public string ville { get; set; }
    public string dateEmbauche { get; set; }
}