Отображение ответа Json от API на модель - PullRequest
0 голосов
/ 03 мая 2020

У меня есть ответ от 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

                }

Я просто не уверен, где отсюда go. Может кто-то указать мне верное направление? Кроме того, если бы вы могли подтвердить, что класс модели правильный, я был бы признателен. Спасибо

1 Ответ

1 голос
/ 03 мая 2020

Мне не нравятся внутренние классы, поэтому я думаю, что модель должна быть:

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; }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...