反序列化JSon对象错误

时间:2016-11-12 10:36:47

标签: c# json parsing json.net

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

String s = File.ReadAllText(@"C:\Users\User\Desktop/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; }
}

仍然无法正常工作,我收到错误:

  

类型&#39; Newtonsoft.Json.JsonReaderException&#39;未处理的异常发生在Newtonsoft.Json.dll。

这是JSON结果:link

由于

2 个答案:

答案 0 :(得分:1)

json包含列表包含openfda属性的结果。以及可以包含多个openfdabrand_name的{​​{1}}。

因为您要求只获取这两个属性,所以我的示例仅基于这两个属性。

强类型

类:

generic_name

反序列化:

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

答案 1 :(得分: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; }
}