我的JsonConvert.DeserailizeObject参数正确吗?

时间:2018-09-06 03:27:04

标签: c# json xamarin

我正在创建一个xamarin应用程序,该应用程序向rest api发送请求。我一直在遵循教程,而且一切似乎都井井有条,但是当我进入JsonConvert.DeserializeObjects时,它将引发错误。

我的主要问题是,我的班级和http请求是否设置正确?

这是我用json2csharp制成的json的类

    public class Results
    {
        public int skip { get; set; }
        public int limit { get; set; }
        public int total { get; set; }
    }

    public class Meta
    {
        public string disclaimer { get; set; }
        public string terms { get; set; }
        public string license { get; set; }
        public string last_updated { get; set; }
        public Results results { get; set; }
    }

    public class Openfda
    {
        public List<string> product_ndc { get; set; }
        public List<bool> is_original_packager { get; set; }
        public List<string> package_ndc { get; set; }
        public List<string> generic_name { get; set; }
        public List<string> spl_set_id { get; set; }
        public List<string> brand_name { get; set; }
        public List<string> manufacturer_name { get; set; }
        public List<string> unii { get; set; }
        public List<string> rxcui { get; set; }
        public List<string> spl_id { get; set; }
        public List<string> substance_name { get; set; }
        public List<string> product_type { get; set; }
        public List<string> route { get; set; }
        public List<string> application_number { get; set; }
    }

    public class Result
    {
        public string effective_time { get; set; }
        public List<string> spl_unclassified_section_table { get; set; }
        public List<string> contraindications { get; set; }
        public List<string> precautions { get; set; }
        public List<string> warnings { get; set; }
        public List<string> description { get; set; }
        public List<string> spl_product_data_elements { get; set; }
        public Openfda openfda { get; set; }
        public string version { get; set; }
        public List<string> dosage_and_administration { get; set; }
        public List<string> adverse_reactions { get; set; }
        public List<string> spl_unclassified_section { get; set; }
        public List<string> how_supplied_table { get; set; }
        public List<string> how_supplied { get; set; }
        public List<string> package_label_principal_display_panel { get; set; }
        public List<string> indications_and_usage { get; set; }
        public List<string> clinical_pharmacology { get; set; }
        public string set_id { get; set; }
        //public string id { get; set; }
        public List<string> overdosage { get; set; }
    }

    public class RootObject
    {
        public Meta meta { get; set; }
        public List<Result> results { get; set; }
    }

这是我的代码,用于创建对实际API的调用

 HttpClient client = new HttpClient();


        var baseURI = new Uri("https://api.fda.gov/drug/label.json?search=openfda.package_ndc:" + pullText.Text + "&limit=1");

        client.BaseAddress = baseURI;

        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        HttpResponseMessage resp = client.GetAsync(baseURI).Result;

        var dataObjects = resp.Content.ReadAsStringAsync().Result;

        var otherView = JsonConvert.DeserializeObject<Openfda>("{\"results\":[\"openfda\":{\"product_ndc\":");

2 个答案:

答案 0 :(得分:0)

  

这是我用json2csharp制成的json的类

使用json2csharp时,应将json字符串反序列化为RootObject,因为该工具会根据您输入的json字符串生成类并映射所有对象。 RootObject是json字符串中的主要对象。

var otherView = JsonConvert.DeserializeObject<RootObject>("YOUR_JSON_STRING");

答案 1 :(得分:0)

您可以将以下代码用于输出,

 var otherView = JsonConvert.DeserializeObject<RootObject>("{\"results\":[\"openfda\":{\"product_ndc\":");
 foreach( var item in otherView.Result)
 {
           // item.Openfda
  }