【发布时间】: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\":");
【问题讨论】:
-
实际上您的输入字符串与
OpenFda的serialize string的格式不同,如果您创建输入字符串的JObject,然后将jObj["results"]["openfda"]传递给您的DeserializeObject方法它会为你工作。 -
首先
{\"results\":[\"openfda\":{\"product_ndc\":json字符串中只有keys。它应该是JsonConvert.DeserializeObject<RootObject>。