下面是我的JSON字符串如何反序列化給JSON字符串到一個定義的類
JSON字符串
{
"RestResponse": {
"messages": [
"Country found matching code [IN]."
],
"result": {
"name": "India",
"alpha2_code": "IN",
"alpha3_code": "IND"
}
}
}
我在Xamarin使這些類,但沒有解析JSON的對象,請指導。
public class Country
{
[JsonProperty(PropertyName = "RestResponse")]
public List<myRestResponse> RestResponse { get; set; }
}
public class myRestResponse
{
[JsonProperty(PropertyName = "messages")]
public List<string> messages { get; set; }
[JsonProperty(PropertyName = "result")]
public List<Result> result { get; set; }
}
public class Result
{
[JsonProperty(PropertyName = "name")]
public string name { get; set; }
[JsonProperty(PropertyName = "alpha2_code")]
public string alpha2_code { get; set; }
[JsonProperty(PropertyName = "alpha3_code")]
public string alpha3_code { get; set; }
}
我使用下面的代碼反序列化
var content = await response.Content.ReadAsStringAsync();
Country country = JsonConvert.DeserializeObject<Country>(content);
'RestResponse'不是首發的集合,並且也不是'result'。 – juharr