【问题标题】:Are my JsonConvert.DeserailizeObject parameters correct?我的 JsonConvert.DeserailizeObject 参数是否正确?
【发布时间】:2018-09-06 03:27:03
【问题描述】:

我正在创建一个 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\":");

【问题讨论】:

  • 实际上您的输入字符串与OpenFdaserialize string的格式不同,如果您创建输入字符串的JObject,然后将jObj["results"]["openfda"]传递给您的DeserializeObject方法它会为你工作。
  • 首先{\"results\":[\"openfda\":{\"product_ndc\":json字符串中只有keys。它应该是JsonConvert.DeserializeObject&lt;RootObject&gt;

标签: c# json xamarin


【解决方案1】:

这是我用 json2csharp 制作的 json 类

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

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

【讨论】:

  • RootObject 应该只是你最终命名类的占位符,否则你最终会得到多个冲突的 RootObject 用于不同的有效负载。
【解决方案2】:

您可以使用以下代码进行输出,

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

【讨论】:

    猜你喜欢
    • 2019-11-15
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 2014-04-29
    相关资源
    最近更新 更多