繁体   English   中英

在统一 3D 上获取 JSON 响应 c# 的选定值

[英]Get selected values of JSON response c# on unity 3D

我是 JSON 新手,我需要从 JSON 响应中获取选定的值。这是我下面的代码,请帮助我使用 narrativa covid19 api

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);

这是我的课程,我使用过去的特殊课程从此链接在 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/
  2. 您没有正确使用 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM