21240

Deserialize JSon Object error

Question:

Hi I need your help for Deserialize Json Object. This is The code I wrote,

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

and this is the class for the 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; } }

and still It doesnt working, I'm getting the error:

<blockquote>

An unhandled exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll.

</blockquote>

This is the JSON results: <a href="https://api.fda.gov/drug/label.json?search=brand_name:humalog&limit=15" rel="nofollow">link</a>

Thanks

Answer1:

The json contains a <strong>list</strong> of results that contain a openfda property. As wel as a openfda that can contain more then one brand_name and generic_name.

Because you are asking to only get these two properties, my example is based on only those two.

<h2>Strongly typed</h2>

Classes:

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; } }

Deserializing:

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

<hr /><h2>Dynamic</h2>

The dynamic way (without strong typed properties):

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]);

Answer2:

The JSON data does not fit to your class structure. You can use <a href="http://json2csharp.com/" rel="nofollow">http://json2csharp.com/</a> to get help for this task. With the provided sample data the data classes look like

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

<strong>Generated Classes</strong>

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; } }

Recommend

  • Convert PS files to PDF/A via Ghostscript, color space problems
  • DDD and Entity Framework classes
  • How would I make an equivalent of this PHP line in bash?
  • Convert Byte Array from Action Script to Image in Java and save it
  • `react-native run-ios` returns Error: Could not find iPhone X simulator
  • Fast method to rotate image in .NET Compact Framework in C#
  • Why is the output of print in python2 and python3 different with the same string?
  • change braces postion with sed
  • freegeoip doesn't work anymore
  • enumerating all possible strings of length K from an alphabet in Python [duplicate]
  • Fast vectorized conversion from RGB to BGRA
  • RecyclerView scrolling top when loading new data
  • set and get value using Session storage for html drop down with jquery
  • Referencing dictionaries in Python
  • Dynamically derive a class in python [duplicate]
  • Create new column from specific rows in pandas dataframe
  • Left factoring grammar
  • How to find and replace, skipping lines containing a particular character?
  • Atmel SAM3X dual bank switching not working
  • Why is this regex failing when adding anchors?
  • How to escape quotation marks in WinDbg's startup command line opening a dump
  • jquery ui datepicker error on beforeShowDay
  • Synchronous XHR deprecation
  • Play HTML audio in Internet Explorer?
  • ViewData, ViewBag and TempData violates MVC? [closed]
  • Getting EOFError along with exceptions when using ftplib
  • Why does this empty dict break shared references?
  • Ruby and class variables in inherit class
  • gspread or such: help me get cell coordinates (not value)
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • Database structure design with variable amounts of fields
  • Q promise. Difference between .when and .then
  • How do I fake an specific browser client when using Java's Net library?
  • How to get a value (ex: baseURL) in every Karate feature?
  • C# - Getting references of reference
  • Turn off referential integrity in Derby? is it possible?
  • Add sale price programmatically to product variations
  • Checking variable from a different class in C#
  • Unable to use reactive element in my shiny app
  • How do I use LINQ to get all the Items that have a particular SubItem?