#c# #unity3d
#c# #unity3d
Вопрос:
Я новичок в JSON, и мне нужно получить выбранные значения из ответа JSON.это мой код ниже, пожалуйста, помогите мне, я использую API narrativa covid19
https://api.covid19tracking.narrativa.com/api/2020-03-22/country/:countryname «
var client = new RestClient("https://api.covid19tracking.narrativa.com/api/2020-03-22/country/" "us");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
Debug.Log(response.Content);
string jsonStringDet = responseCountries.Content;
Rootobject myDeserializedClass = JsonConvert.DeserializeObject<Rootobject>(jsonStringDet);
и это мой класс, который я использую past special, чтобы получить этот класс в visula studio по этой ссылке
https://api.covid19tracking.narrativa.com/api/2020-11-02/country/us
public class Rootobject
{
public Dates dates { get; set; }
public Metadata metadata { get; set; }
public Total total { get; set; }
public string updated_at { get; set; }
}
public class Dates
{
public _20201102 _20201102 { get; set; }
}
public class _20201102
{
public Countries countries { get; set; }
public Info info { get; set; }
}
public class Countries
{
public US US { get; set; }
}
public class US
{
public string date { get; set; }
public string id { get; set; }
public Link[] links { get; set; }
public string name { get; set; }
public string name_es { get; set; }
public string name_it { get; set; }
public Region[] regions { get; set; }
public string source { get; set; }
public int today_confirmed { get; set; }
public int today_deaths { get; set; }
public int today_new_confirmed { get; set; }
public int today_new_deaths { get; set; }
public int today_new_open_cases { get; set; }
public int today_new_recovered { get; set; }
public int today_open_cases { get; set; }
public int today_recovered { get; set; }
public float today_vs_yesterday_confirmed { get; set; }
public float today_vs_yesterday_deaths { get; set; }
public float today_vs_yesterday_open_cases { get; set; }
public float today_vs_yesterday_recovered { get; set; }
public int yesterday_confirmed { get; set; }
public int yesterday_deaths { get; set; }
public int yesterday_open_cases { get; set; }
public int yesterday_recovered { get; set; }
}
public class Link
{
public string href { get; set; }
public string rel { get; set; }
public string type { get; set; }
}
public class Region
{
public string date { get; set; }
public string id { get; set; }
public Link1[] links { get; set; }
public string name { get; set; }
public string name_es { get; set; }
public string name_it { get; set; }
public string source { get; set; }
public Sub_Regions[] sub_regions { get; set; }
public int today_confirmed { get; set; }
public int today_deaths { get; set; }
public int today_new_confirmed { get; set; }
public int today_new_deaths { get; set; }
public int today_new_open_cases { get; set; }
public int today_new_recovered { get; set; }
public int today_new_tests { get; set; }
public int today_new_total_hospitalised_patients { get; set; }
public int today_open_cases { get; set; }
public int today_recovered { get; set; }
public int today_tests { get; set; }
public int today_total_hospitalised_patients { get; set; }
public float? today_vs_yesterday_confirmed { get; set; }
public float? today_vs_yesterday_deaths { get; set; }
public float today_vs_yesterday_open_cases { get; set; }
public float? today_vs_yesterday_recovered { get; set; }
public float today_vs_yesterday_tests { get; set; }
public float? today_vs_yesterday_total_hospitalised_patients { get; set; }
public int yesterday_confirmed { get; set; }
public int yesterday_deaths { get; set; }
public int yesterday_open_cases { get; set; }
public int yesterday_recovered { get; set; }
public int yesterday_tests { get; set; }
public int yesterday_total_hospitalised_patients { get; set; }
}
public class Link1
{
public string href { get; set; }
public string rel { get; set; }
public string type { get; set; }
}
public class Sub_Regions
{
public string date { get; set; }
public string id { get; set; }
public string name { get; set; }
public string name_es { get; set; }
public string name_it { get; set; }
public string source { get; set; }
public int today_confirmed { get; set; }
public int today_deaths { get; set; }
public int today_new_confirmed { get; set; }
public int today_new_deaths { get; set; }
public int today_new_recovered { get; set; }
public int today_recovered { get; set; }
public float? today_vs_yesterday_confirmed { get; set; }
public float? today_vs_yesterday_deaths { get; set; }
public object today_vs_yesterday_recovered { get; set; }
public int yesterday_confirmed { get; set; }
public int yesterday_deaths { get; set; }
public int yesterday_recovered { get; set; }
}
public class Info
{
public string date { get; set; }
public string date_generation { get; set; }
public string yesterday { get; set; }
}
public class Metadata
{
public string by { get; set; }
public string[] url { get; set; }
}
public class Total
{
public string date { get; set; }
public string name { get; set; }
public string name_es { get; set; }
public string name_it { get; set; }
public string rid { get; set; }
public string source { get; set; }
public int today_confirmed { get; set; }
public int today_deaths { get; set; }
public int today_new_confirmed { get; set; }
public int today_new_deaths { get; set; }
public int today_new_open_cases { get; set; }
public int today_new_recovered { get; set; }
public int today_open_cases { get; set; }
public int today_recovered { get; set; }
public float today_vs_yesterday_confirmed { get; set; }
public float today_vs_yesterday_deaths { get; set; }
public float today_vs_yesterday_open_cases { get; set; }
public float today_vs_yesterday_recovered { get; set; }
public int yesterday_confirmed { get; set; }
public int yesterday_deaths { get; set; }
public int yesterday_open_cases { get; set; }
public int yesterday_recovered { get; set; }
}
Ответ №1:
- вы должны правильно создавать классы, которые представляют ваш ответ, для этого я бы порекомендовал этот сайт, который поможет в этом: (https://json2csharp.com /)
- вы неправильно используете RestSharp, вы можете использовать универсальную версию Execute, которая принимает модель для анализа отправленного json
var client = new RestClient("https://api.covid19tracking.narrativa.com/api/2020-03-22/country/" "us");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
var response = client.Execute<Rootobject>(request)?.Data;
чтобы получить доступ к вашему объекту Dates, вам нужно только сделать это,
var dates = response?.dates
Комментарии:
1. Я пробую это, но я не работаю, и да, я использую json2csharp.com и всегда у меня есть эта ошибка: Исключение NullReferenceException: ссылка на объект не установлена для экземпляра объекта
2. Если вы используете сайт, о котором я упоминал, пожалуйста, укажите обложку с атрибутами. если вы использовали мой код, у вас не может быть исключения с нулевой ссылкой.
3. да, это работает, но моя проблема в том, как выбрать данные из результата ответа
4. у вас уже есть свой ответ, приведенный к вашей модели, поэтому вы можете получить доступ к его свойствам.
5. Я получил эту ошибку NullReferenceException: ссылка на объект не установлена для экземпляра объекта test. Start () (в Assets/Scripts/test.cs:40)