如何将json字符串反序列化到模型中?

问题描述 投票:0回答:2

大家好

[一种json模型,如下所示,使我通过api返回。

 {
  "data": [
    {
      "countryStats": {
        "order": 2,
        "confirmedCount": 15679,
        "deathCount": 277,
        "recovryCount": 333,
        "name": "Türkiye"
      },
      "cityStats": [

      ]
    },
    {
      "countryStats": {
        "order": 1,
        "confirmedCount": 216722,
        "deathCount": 5138,
        "recovryCount": 8672,
        "name": "Amerika Birleşik Devletleri"
      },
      "cityStats": [
        {
          "order": 1,
          "confirmedCount": 84070,
          "deathCount": 1941,
          "recovryCount": 0,
          "name": "New York"
        },..

我正在使用下面的代码提取此json数据。

static string GET(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            try
            {
                WebResponse response = request.GetResponse();
                using (Stream responseStream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
                    return reader.ReadToEnd();
                }
            }
            catch (WebException ex)
            {
                WebResponse errorResponse = ex.Response;
                using (Stream responseStream = errorResponse.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8"));
                    String errorText = reader.ReadToEnd();
                    // log errorText
                }
                throw;
            }
        }

这是给我的一个字符串表达式。我想将此字符串表达式分配给我的模型,该怎么做。

我的模特

public class CountryStats
    {
        public int Order  { get; set; }
        public int ConfirmedCount { get; set; }
        public int DeathCount { get; set; }
        public int RecovryCount { get; set; }
        public string Name { get; set; }
        public List<CountryStats> CityStats { get; set; }
    }

我正在尝试像这样翻译它

var result = JsonConvert.DeserializeObject<List<CountryStats>>(res);

但是我得到一个错误

enter image description here

c# json serializer
2个回答
1
投票

问题是您的模型。它应如下所示。

public class CountryStatsCollection
{
    public List<ContryStats> Data { get; set }
}

public class CountryStats
    {
        public int Order  { get; set; }
        public int ConfirmedCount { get; set; }
        public int DeathCount { get; set; }
        public int RecovryCount { get; set; }
        public string Name { get; set; }
        public List<CountryStats> CityStats { get; set; }
    }

0
投票

您必须更改

public class BaseClass
{
    public List<CountryStats> Data { get; set; }
}
public class CountryStats
{
    public int Order  { get; set; }
    public int ConfirmedCount { get; set; }
    public int DeathCount { get; set; }
    public int RecovryCount { get; set; }
    public string Name { get; set; }
    public List<CountryStats> CityStats { get; set; }
}

然后

var result = JsonConvert.DeserializeObject<BaseClass>(res);
© www.soinside.com 2019 - 2024. All rights reserved.