【问题标题】:Mapping a Json response from an API to a Model将 Json 响应从 API 映射到模型
【发布时间】:2020-05-03 18:16:53
【问题描述】:

我收到了来自类似这样的 API 的响应

{
    "Global": {
        "NewConfirmed": 53041,
        "TotalConfirmed": 2142221,
        "NewDeaths": 3583,
        "TotalDeaths": 168885,
        "NewRecovered": 40194,
        "TotalRecovered": 1005926
    },
    "Countries": [
        {
            "Country": "ALA Aland Islands",
            "CountryCode": "AX",
            "Slug": "ala-aland-islands",
            "NewConfirmed": 0,
            "TotalConfirmed": 0,
            "NewDeaths": 0,
            "TotalDeaths": 0,
            "NewRecovered": 0,
            "TotalRecovered": 0,
            "Date": "2020-05-03T17:59:05Z"
        },
        {
            "Country": "Afghanistan",
            "CountryCode": "AF",
            "Slug": "afghanistan",
            "NewConfirmed": 134,
            "TotalConfirmed": 2469,
            "NewDeaths": 4,
            "TotalDeaths": 72,
            "NewRecovered": 21,
            "TotalRecovered": 331,
            "Date": "2020-05-03T17:59:05Z"
        },
        {
            "Country": "Albania",
            "CountryCode": "AL",
            "Slug": "albania",
            "NewConfirmed": 7,
            "TotalConfirmed": 789,
            "NewDeaths": 0,
            "TotalDeaths": 31,
            "NewRecovered": 31,
            "TotalRecovered": 519,
            "Date": "2020-05-03T17:59:05Z"
        }
}

我需要将结果映射到我的 Model 类,看起来像这样

 public class Cases
    {
        public class Global
        {
            [JsonProperty("NewConfirmed")]
            public int NewConfirmed { get; set; }

            [JsonProperty("TotalConfirmed")]
            public int TotalConfirmed { get; set; }

            [JsonProperty("NewDeaths")]
            public int NewDeaths { get; set; }

            [JsonProperty("TotalDeaths")]
            public int TotalDeaths { get; set; }

            [JsonProperty("NewRecovered")]
            public int NewRecovered { get; set; }

            [JsonProperty("TotalRecovered")]
            public int TotalRecovered { get; set; }

            [JsonProperty("Countries")]
            public Country[] countries { get; set; }
        }

        public class Country
        {
            [JsonProperty("Country")]
            public string country { get; set; }

            [JsonProperty("CountryCode")]
            public string countryCode { get; set; }


            [JsonProperty("NewConfirmed")]
            public int NewConfirmed { get; set; }

            [JsonProperty("TotalConfirmed")]
            public int TotalConfirmed { get; set; }

            [JsonProperty("NewDeaths")]
            public int NewDeaths { get; set; }

            [JsonProperty("TotalDeaths")]
            public int TotalDeaths { get; set; }

            [JsonProperty("NewRecovered")]
            public int NewRecovered { get; set; }

            [JsonProperty("TotalRecovered")]
            public int TotalRecovered { get; set; }


        }
        public class Countries
        {
            [JsonProperty("Global")]
            public Cases.Global global { get; set; }

            [JsonProperty("Countries")]
            public Country[] countries { get; set; }
        }

    }

A 有一个 void Get 方法,如下所示:

 //create a web request object
                WebRequest reqObject = WebRequest.Create(apiUrl);

                //request method
                reqObject.Method = "GET";

                //create a response object
                HttpWebResponse httpWebResponseObj = (HttpWebResponse)reqObject.GetResponse();

                //capture the response stream            
                using (Stream stream = httpWebResponseObj.GetResponseStream())
                {
                    //stream reader -> pass in stream
                    StreamReader streamReader = new StreamReader(stream);

                    //grab the stream response and put it in a strign variable
                    string readResponse = streamReader.ReadToEnd();

                    //close the streamreader
                    streamReader.Close();

                    //deserialize stream reader
                    var deserializedObj = JsonConvert.DeserializeObject<Cases>(readResponse);

                   //foreach here

                }

我只是不知道从这里去哪里。谁能指出我正确的方向?另外,如果您能确认模型类是正确的,我将不胜感激。 谢谢

【问题讨论】:

  • 反序列化后为什么会出现这个Convert.ToString(...)?如果您的映射正确,JsonConvert.DeserializeObject(...) 应该会给您预期的结果(= Cases 实例)
  • 添加语言标签可能有用。
  • @TheFool 是 c#。我已经添加了
  • @OlivierDepriester 该变量之前是一个字符串。当我将其更改为 var 时忘记摆脱转换。

标签: c# json api


【解决方案1】:

我不喜欢内部类,所以我的想法是模型应该是:

public class Cases
{
    // "Global" section
    [JsonProperty("Global")]
    public Global Global { get; set; }

    // "Countries" : list of Country
    [JsonProperty("Countries")]
    public List<Country> Countries { get; set; }
}

public class Global
{
    [JsonProperty("NewConfirmed")]
    public int NewConfirmed { get; set; }

    [JsonProperty("TotalConfirmed")]
    public int TotalConfirmed { get; set; }

    [JsonProperty("NewDeaths")]
    public int NewDeaths { get; set; }

    [JsonProperty("TotalDeaths")]
    public int TotalDeaths { get; set; }

    [JsonProperty("NewRecovered")]
    public int NewRecovered { get; set; }

    [JsonProperty("TotalRecovered")]
    public int TotalRecovered { get; set; }
}


// !!! You did not map "Slug" and "Date" !!!
public class Country
{

    [JsonProperty("Country")]
    public string country { get; set; }

    [JsonProperty("CountryCode")]
    public string countryCode { get; set; }

    [JsonProperty("NewConfirmed")]
    public int NewConfirmed { get; set; }

    [JsonProperty("TotalConfirmed")]
    public int TotalConfirmed { get; set; }

    [JsonProperty("NewDeaths")]
    public int NewDeaths { get; set; }

    [JsonProperty("TotalDeaths")]
    public int TotalDeaths { get; set; }

    [JsonProperty("NewRecovered")]
    public int NewRecovered { get; set; }

    [JsonProperty("TotalRecovered")]
    public int TotalRecovered { get; set; }
}

【讨论】:

  • 感谢 Model 类重新排列。一旦我这样做并将其更正为deserializedObj,它就会正确映射所有内容。
猜你喜欢
  • 2018-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-23
  • 1970-01-01
  • 2020-09-13
  • 2019-02-12
  • 1970-01-01
相关资源
最近更新 更多