Hi Guys
I really appreciate the help parsing out this JSON result
{"result_count":1,"results":[{"created_epoch":"1151241154000","enumeration_type":"NPI-1","last_updated_epoch":"1630682512000","number":"1427084888","addresses":[{"country_code":"US","country_name":"United States","address_purpose":"LOCATION","address_type":"DOM","address_1":"6101 WEBB RD STE 301","city":"TAMPA","state":"FL","postal_code":"336152866","telephone_number":"813-885-3600","fax_number":"813-885-4600"},{"country_code":"US","country_name":"United States","address_purpose":"MAILING","address_type":"DOM","address_1":"6101 WEBB RD STE 301","city":"TAMPA","state":"FL","postal_code":"336152866"}],"practiceLocations":[],"basic":{"first_name":"ALEJANDRO","last_name":"CINTAS","middle_name":"R","credential":"MD","sole_proprietor":"NO","gender":"M","enumeration_date":"2006-06-25","last_updated":"2021-09-03","certification_date":"2021-09-03","status":"A"},"taxonomies":[{"code":"207R00000X","taxonomy_group":"","desc":"Internal Medicine","state":"FL","license":"ME0080234","primary":false},{"code":"207Q00000X","taxonomy_group":"","desc":"Family Medicine","state":"FL","license":"ME80234","primary":true}],"identifiers":[{"code":"05","desc":"MEDICAID","issuer":null,"identifier":"259401300","state":"FL"}],"endpoints":[],"other_names":[]}]}
Here it is my code I only posting a few of the result but I would like to get them all value from the JSON string.
Physicians DoctorInfo = new Physicians();
string info = new WebClient().DownloadString(apiUrl);
JavaScriptSerializer jsonObject = new JavaScriptSerializer();
DoctorInfo = jsonObject.Deserialize(info);
//- Result values are coming as null for all the properties
Console.WriteLine(DoctorInfo.number);
internal class Physicians
{
public string first_name { get; set; }
public string last_name { get; set; }
//country
public string country_code { get; set; }
public string country_name { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postal_code { get; set; }
public string telephone_number { get; set; }
public string fax_number { get; set; }
public string number { get; set; }
public string TimeZone { get; set; }
}Thanks
JS.

Vishal YelveRank #178 · 13.6K reputationPosted Oct 23, 2023, 2:12 AM EDT
Hi Jose,
Try this
public class Rootobject { public int result_count { get; set; } public List results { get; set; } } public class Result { public string created_epoch { get; set; } public string enumeration_type { get; set; } public string last_updated_epoch { get; set; } public string number { get; set; } public List addresses { get; set; } public object[] practiceLocations { get; set; } public Basic basic { get; set; } public List taxonomies { get; set; } public List identifiers { get; set; } public object[] endpoints { get; set; } public object[] other_names { get; set; } } public class Basic { public string first_name { get; set; } public string last_name { get; set; } public string middle_name { get; set; } public string credential { get; set; } public string sole_proprietor { get; set; } public string gender { get; set; } public string enumeration_date { get; set; } public string last_updated { get; set; } public string certification_date { get; set; } public string status { get; set; } } public class Address { public string country_code { get; set; } public string country_name { get; set; } public string address_purpose { get; set; } public string address_type { get; set; } public string address_1 { get; set; } public string city { get; set; } public string state { get; set; } public string postal_code { get; set; } public string telephone_number { get; set; } public string fax_number { get; set; } } public class Taxonomy { public string code { get; set; } public string taxonomy_group { get; set; } public string desc { get; set; } public string state { get; set; } public string license { get; set; } public bool primary { get; set; } } public class Identifier { public string code { get; set; } public string desc { get; set; } public bool issuer { get; set; } public string identifier { get; set; } public string state { get; set; } } // in your main code string info = new WebClient().DownloadString(apiUrl); // Deserialize the JSON string var root = JsonConvert.DeserializeObject(info);Sam HobbsRank #107 · 20.5K reputationPosted Oct 22, 2023, 5:22 PM EDT
In Visual Studio open or create a C# file (cs extension). Copy some sample JSON (such as what you posted here) to the clipboard. In Visual Studio, in the Edit menu, Choose "Paste Special" | "Paste JSON As Classes". VS will generate the classes you need. Then you can use the .Net classes in the System.Text.Json namespace or the classes in the Newtonsoft library to parse JSON.
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Oct 22, 2023, 5:02 PM EDT
To parse the provided json, use the following class structure:
public class Root { public int result_count { get; set; } public List results { get; set; } } public class Result { public string created_epoch { get; set; } public string enumeration_type { get; set; } public string last_updated_epoch { get; set; } public string number { get; set; } public List addresses { get; set; } public List practiceLocations { get; set; } public Basic basic { get; set; } public List taxonomies { get; set; } public List identifiers { get; set; } public List endpoints { get; set; } public List other_names { get; set; } } public class Address { public string country_code { get; set; } public string country_name { get; set; } public string address_purpose { get; set; } public string address_type { get; set; } public string address_1 { get; set; } public string city { get; set; } public string state { get; set; } public string postal_code { get; set; } public string telephone_number { get; set; } public string fax_number { get; set; } } public class Basic { public string first_name { get; set; } public string last_name { get; set; } public string middle_name { get; set; } public string credential { get; set; } public string sole_proprietor { get; set; } public string gender { get; set; } public string enumeration_date { get; set; } public string last_updated { get; set; } public string certification_date { get; set; } public string status { get; set; } } public class Taxonomy { public string code { get; set; } public string taxonomy_group { get; set; } public string desc { get; set; } public string state { get; set; } public string license { get; set; } public bool primary { get; set; } } public class Identifier { public string code { get; set; } public string desc { get; set; } public object issuer { get; set; } public string identifier { get; set; } public string state { get; set; } } static void Main(string[] args) { string info = new WebClient().DownloadString(apiUrl); // Deserialize the JSON string var root = JsonConvert.DeserializeObject(info); }Vishal JoshiRank #377 · 6.7K reputationPosted Oct 23, 2023, 2:00 AM EDT
Hello
To parse dynamic JSON in C#, you can use the System.Text.Json.JsonDocument class provided by the System.Text.Json namespace, which is available in .NET Core and .NET 5.
Here's an example of how to parse dynamic JSON using JsonDocument:
using System; using System.Text.Json; namespace DynamicJsonParsing { class Program { static void Main(string[] args) { // Sample JSON string string jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}"; // Parse the JSON string into a JsonDocument JsonDocument doc = JsonDocument.Parse(jsonString); // Access elements dynamically JsonElement root = doc.RootElement; string name = root.GetProperty("name").GetString(); int age = root.GetProperty("age").GetInt(); string city = root.GetProperty("city").GetString(); // Display the parsed values Console.WriteLine("Name: " + name); Console.WriteLine("Age: " + age); Console.WriteLine("City: " + city); } } }Thanks
Vishal Joshi
Jose SaizRank #5,346 · 302 reputationPosted Oct 23, 2023, 7:05 AM EDT
Thank you guys for all your help, I was wondering why I was getting result value as null, now I see why I was not parsing out the JSON file correctly, I needed to split the file by [Result], [Basic], [Address], [Taxonomy] etc, I thanks very much Vishal Yelve and Muhammad Imran Ansari for the great explanation which worked 100% and for letting know HOW TO. I've parsed JSON before but just simple straight forward not like this one with so many levels.
I also thanks Sam and Vishal for their replies.
Guys I really appreciate all your help.