我已经看过很多其他帖子,解决方案总是相同的,但它不适用于我,所以我在这里。无法从提取的json中自动生成C#对象
我有以下HttpClient
提取JSON字符串并将其存储在一个变量。
JSON
{"visiteur":{"id":"a17","nom":"Andre","prenom":"David","login":"dandre","mdp":"12e0b9be32932a8028b0ef0432a0a0a99421f745","adresse":"1 rue Petit","cp":"46200","ville":"Lalbenque","dateEmbauche":"1998-11-23","idTypeActeur":"2","oldMdp":"oppg5","mail":"[email protected]","questionSecrete":"GSB","version":"0"}}
客户
// url de récupération du json de l'acteur
string urlActeur = "http://10.0.0.5/ppe3JoJuAd/gsbAppliFraisV2/webservices/w_visiteur.php" + "?" + "login=" + Login + "&" + "pass=" + Pass;
//instanciation du client http qui envoi un header json
HttpClient clientActeur = new HttpClient();
clientActeur.DefaultRequestHeaders.Accept.Clear();
clientActeur.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//réponse à la requête Http
var response = await clientActeur.GetAsync(urlActeur);
var json = response.Content.ReadAsStringAsync().Result;
var acteurJson = JsonConvert.DeserializeObject<ActeurJson>(json);
这里是JSON变量的内容:
{"visiteur":{"id":"a17","nom":"Andre","prenom":"David","login":"dandre","mdp":"12e0b9be32932a8028b0ef0432a0a0a99421f745","adresse":"1 rue Petit","cp":"46200","ville":"Lalbenque","dateEmbauche":"1998-11-23","idTypeActeur":"2","oldMdp":"oppg5","mail":"[email protected]","questionSecrete":"GSB","version":"0"}}
所以确实获取JSON,并将其存储在变量中,但看着我的acteurJson
变量,我得到一个null
对象。
类与json2csharp验证:
public class Acteur
{
[JsonProperty("id")]
public string id { get; set; }
[JsonProperty("nom")]
public string nom { get; set; }
[JsonProperty("prenom")]
public string prenom { get; set; }
[JsonProperty("login")]
public string login { get; set; }
[JsonProperty("mdp")]
public string mdp { get; set; }
[JsonProperty("adresse")]
public string adresse { get; set; }
[JsonProperty("cp")]
public string cp { get; set; }
[JsonProperty("ville")]
public string ville { get; set; }
[JsonProperty("dateEmbauche")]
public string dateEmbauche { get; set; }
[JsonProperty("idTypeActeur")]
public string idTypeActeur { get; set; }
[JsonProperty("oldMdp")]
public string oldMdp { get; set; }
[JsonProperty("mail")]
public string mail { get; set; }
[JsonProperty("questionSecrete")]
public string questionSecrete { get; set; }
[JsonProperty("version")]
public string version { get; set; }
}
public class ActeurJson
{
public Acteur Acteur { get; set; }
}
我试图从JSON变量得到JSON自动生成一个Acteur
对象。我错过了什么?