反序列化JSon对象错误



你好,我需要你的帮助来反序列化Json对象。这是我写的代码,

String s = File.ReadAllText(@"C:UsersUserDesktop/json1.json");
var myfields = Newtonsoft.Json.JsonConvert.DeserializeObject<YourTwoField>(s);
Console.WriteLine(myfields.FieldOne);

这是JSON OBJECT

的类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;

public class YourTwoField
{
    [JsonProperty("brand_name")]
    public List<string> FieldOne { get; set; }
    [JsonProperty("generic_name")]
    public List<string> FieldTwo { get; set; }
}

仍然不工作,我得到错误:

类型为'Newtonsoft.Json '的未处理异常。JsonReaderException'发生在Newtonsoft.Json.dll.

这是JSON的结果:link

谢谢

json包含一个列表包含openfda属性的结果。以及可以包含多于一个brand_namegeneric_nameopenfda

因为你只要求得到这两个属性,所以我的例子只基于这两个属性。

强类型

类:

public class Openfda
{     
    [JsonProperty("generic_name")]
    public List<string> GenericName { get; set; }
    [JsonProperty("brand_name")]
    public List<string> BrandName { get; set; }     
}
public class Result
{
    [JsonProperty("openfda")]
    public Openfda Openfda { get; set; }   
}
public class Root
{
    [JsonProperty("results")]
    public List<Result> Results { get; set; }
}

反序列化:

var myfields = Newtonsoft.Json.JsonConvert.DeserializeObject<Root>(json);
Console.WriteLine(myfields.Results[0].Openfda.BrandName[0]);
Console.WriteLine(myfields.Results[0].Openfda.GenericName[0]);


动态

dynamic方式(不含强类型属性):

dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
Console.WriteLine(obj.results[0].openfda.brand_name[0]);
Console.WriteLine(obj.results[0].openfda.generic_name[0]);

JSON数据不适合您的类结构。您可以使用http://json2csharp.com/获取有关此任务的帮助。使用提供的样例数据,数据类看起来像

...
var myfields = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(s);
...

生成的类

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<string> nui { 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> pharm_class_cs { get; set; }
    public List<string> brand_name { get; set; }
    public List<string> original_packager_product_ndc { get; set; }
    public List<string> manufacturer_name { get; set; }
    public List<string> unii { 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 List<string> pharm_class_epc { get; set; }
    public List<bool?> is_original_packager { get; set; }
    public List<string> upc { get; set; }
    public List<string> rxcui { get; set; }
}
public class Result
{
    public string effective_time { get; set; }
    public List<string> spl_unclassified_section_table { get; set; }
    public List<string> inactive_ingredient { get; set; }
    public List<string> instructions_for_use { get; set; }
    public List<string> purpose { get; set; }
    public List<string> keep_out_of_reach_of_children { get; set; }
    public List<string> spl_patient_package_insert { 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> spl_unclassified_section { get; set; }
    public List<string> storage_and_handling { get; set; }
    public List<string> package_label_principal_display_panel { get; set; }
    public List<string> indications_and_usage { get; set; }
    public string set_id { get; set; }
    public string id { get; set; }
    public List<string> spl_patient_package_insert_table { get; set; }
    public List<string> active_ingredient { get; set; }
    public List<string> drug_interactions { get; set; }
    public List<string> geriatric_use { get; set; }
    public List<string> precautions { get; set; }
    public List<string> pharmacodynamics { get; set; }
    public List<string> general_precautions { get; set; }
    public List<string> pharmacokinetics { get; set; }
    public List<string> teratogenic_effects { get; set; }
    public List<string> dosage_and_administration_table { get; set; }
    public List<string> pediatric_use { get; set; }
    public List<string> contraindications { get; set; }
    public List<string> storage_and_handling_table { get; set; }
    public List<string> pregnancy { get; set; }
    public List<string> nursing_mothers { get; set; }
    public List<string> adverse_reactions { get; set; }
    public List<string> how_supplied_table { get; set; }
    public List<string> laboratory_tests { get; set; }
    public List<string> how_supplied { get; set; }
    public List<string> information_for_patients { get; set; }
    public List<string> clinical_pharmacology { get; set; }
    public List<string> carcinogenesis_and_mutagenesis_and_impairment_of_fertility { get; set; }
    public List<string> overdosage { get; set; }
    public List<string> recent_major_changes { get; set; }
    public List<string> nonclinical_toxicology { get; set; }
    public List<string> dosage_forms_and_strengths { get; set; }
    public List<string> mechanism_of_action { get; set; }
    public List<string> clinical_studies_table { get; set; }
    public List<string> adverse_reactions_table { get; set; }
    public List<string> warnings_and_cautions { get; set; }
    public List<string> recent_major_changes_table { get; set; }
    public List<string> animal_pharmacology_and_or_toxicology { get; set; }
    public List<string> use_in_specific_populations { get; set; }
    public List<string> clinical_studies { get; set; }
    public List<string> clinical_pharmacology_table { get; set; }
    public List<string> instructions_for_use_table { get; set; }
    public List<string> pharmacodynamics_table { get; set; }
}
public class RootObject
{
    public Meta meta { get; set; }
    public List<Result> results { get; set; }
}

相关内容

  • 没有找到相关文章

最新更新