我有這個字串需要反序列化。
"{\"errors\":{\"validationError\":[\"Custom error message here.\"]},\"title\":\"One or more validation errors occurred.\",\"status\":400}"
這是我的代碼,我正在使用 XUnit 進行測驗。
var response = await client.GetAsync("api/ABC/Check?draftId=" draftId);
var responseString = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<VerificationResponseError>(responseString);
Assert.Equal("Custom error message here.", result.validationError[0]);
這是我的VerificationResponseError課。
public class VerificationResponseError {
public string errors { get; set; }
public List<string> validationError { get; set; }
}
但是,它在
var result = JsonConvert.DeserializeObject<VerificationResponseError>(responseString);
uj5u.com熱心網友回復:
聽起來您的課程不正確,不應該是這樣嗎?
public class Errors
{
public List<string> validationError { get; set; }
}
public class VerificationResponseError
{
public Errors errors { get; set; }
public string title { get; set; }
public int status { get; set; }
}
您可以使用此工具來驗證https://json2csharp.com/
uj5u.com熱心網友回復:
您的類不代表您的 json 結構。接下來試試:
public class VerificationResponseError
{
public Errors errors { get; set; }
public string title { get; set; }
public int status { get; set; }
}
public class Errors
{
public List<string> validationError { get; set; }
}
var result = JsonConvert.DeserializeObject<VerificationResponseError>(responseString);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/412333.html
標籤:
下一篇:json陣列中的賽普拉斯推送值
