JSON response string send by client:
{ "{\"user_id\":\"\",\"school_id\":\"1\",\"user_name\":\"pfirstname1#example.com\",\"firstname\":\"pfirstname1\",\"surname\":\"psurname1\",\"type\":\"P\",\"phoneno\":\"1446863\",\"mobileno\":\"3614632\",\"address\":\"mehdipatnam\",\"relation\":\"Father\",\"date_created\":\"\",\"student_dto\":": { "{\"student_id\":0,\"user_id\":0,\"firstname\":\"sfirstname1\",\"surname\":\"ssurname1\",\"rollno\":1,\"class_id\":\"2\",\"school_id\":\"1\",\"address\":\"mehdipatnam\",\"remarks\":\"\",\"date_created\":\"\"}]}": "" }}
Then to deserialize this I'm doing this step:
dynamic data = new JavaScriptSerializer().Deserialize(response.ToString(), typeof(object));
Then when I look in to the debugger I can see:
[0] {[{"user_dto":{"user_id":"","school_id":"1","user_name":"pfirstname1#example.com","firstname":"pfirstname1","surname":"psurname1","type":"P","phoneno":"1446863","mobileno":"3614632","address":"mehdipatnam","relation":"Father","date_created":"","student_dto":, System.Collections.Generic.Dictionary`2[System.String,System.Object]]} System.Collections.Generic.KeyValuePair<string,object>
But how do I access the values?
I'm going into this assuming that the mess you posted as JSON should actually be the following after all the \" and erroneous quotes are removed.
{
"user_id":"",
"school_id":"1",
"user_name":"pfirstname1#example.com",
"firstname":"pfirstname1",
"surname":"psurname1",
"type":"P",
"phoneno":"1446863",
"mobileno":"3614632",
"address":"mehdipatnam",
"relation":"Father",
"date_created":"",
"student_dto":
{
"student_id":0,
"user_id":0,
"firstname":"sfirstname1",
"surname":"ssurname1",
"rollno":1,
"class_id":"2",
"school_id":"1",
"address":"mehdipatnam",
"remarks":"",
"date_created":""
}
}
If that's the case, then json2csharp.com gives the following as the classes to deserialize into:
public class StudentDto
{
public int student_id { get; set; }
public int user_id { get; set; }
public string firstname { get; set; }
public string surname { get; set; }
public int rollno { get; set; }
public string class_id { get; set; }
public string school_id { get; set; }
public string address { get; set; }
public string remarks { get; set; }
public string date_created { get; set; }
}
public class RootObject
{
public string user_id { get; set; }
public string school_id { get; set; }
public string user_name { get; set; }
public string firstname { get; set; }
public string surname { get; set; }
public string type { get; set; }
public string phoneno { get; set; }
public string mobileno { get; set; }
public string address { get; set; }
public string relation { get; set; }
public string date_created { get; set; }
public StudentDto student_dto { get; set; }
}
Now you should be able to deserialize directly into that type.
new JavaScriptSerializer().Deserialize<RootObject>(response.ToString());
Related
I need to deserialize a JSON respons from the FastBill API, (I'm using JSON .NET) I've created the C# class who will "hold" the data retrieved from the API response, the problem is that the response from the server is bad, looks like this:
"{
\"REQUEST\":{
\"SERVICE\":\"invoice.get\",
\"LIMIT\":1,
\"FILTER\":{
\"YEAR\":2018
}
},
\"RESPONSE\":{
\"INVOICES\":[{
\"INVOICE_ID\":\"11586818\",
\"TYPE\":\"outgoing\",
\"CUSTOMER_ID\":\"4735248\",
\"CUSTOMER_NUMBER\":\"3211\",
\"CUSTOMER_COSTCENTER_ID\":\"0\",
\"CONTACT_ID\":\"\",
\"PROJECT_ID\":\"0\",
\"CURRENCY_CODE\":\"EUR\",
\"DELIVERY_DATE\":\"02.01.2018\",
\"INVOICE_TITLE\":\"Rechnung f\\u00fcr Ihre Bestellung bei Amazon\",
\"CASH_DISCOUNT_PERCENT\":\"0.00\",
\"CASH_DISCOUNT_DAYS\":\"0\",
\"SUB_TOTAL\":22.66,
\"VAT_TOTAL\":4.31,
\"VAT_ITEMS\":[{
\"VAT_PERCENT\":\"19.00\",
\"COMPLETE_NET\":22.66,
\"VAT_VALUE\":4.3061344545
}],
\"ITEMS\":[{
\"INVOICE_ITEM_ID\":\"33975736\",
\"ARTICLE_NUMBER\":\"CK-7I70-IBD4\",
\"DESCRIPTION\":\"Gr\\u00fcne Elefanten\\\\'s Bio OPC Traubenkernextrakt mit Bio Acerola Hochdosiert 2 Monatsvorrat - 390mg Kapseln Trauben aus Frankreich\",
\"QUANTITY\":\"1.0000\",
\"UNIT_PRICE\":\"22.66386555\",
\"VAT_PERCENT\":\"19.00\",
\"VAT_VALUE\":4.3061344545,
\"COMPLETE_NET\":22.66386555,
\"COMPLETE_GROSS\":26.9700000045,
\"SORT_ORDER\":1
}],
\"TOTAL\":26.97,
\"ORGANIZATION\":\"\",
\"NOTE\":\"\",
\"SALUTATION\":\"\",
\"FIRST_NAME\":\"NAME\",
\"LAST_NAME\":\"NAME\",
\"ADDRESS\":\"ADDRESS\",
\"ADDRESS_2\":\" \",
\"ZIPCODE\":\"97493\",
\"CITY\":\"CITY\",
\"PAYMENT_TYPE\":\"1\",
\"BANK_NAME\":\"\",
\"BANK_ACCOUNT_NUMBER\":\"\",
\"BANK_CODE\":\"\",
\"BANK_ACCOUNT_OWNER\":\"\",
\"BANK_IBAN\":\"\",
\"BANK_BIC\":\"\",
\"COUNTRY_CODE\":\"DE\",
\"VAT_ID\":\"\",
\"TEMPLATE_ID\":\"963360\",
\"INVOICE_NUMBER\":\"3209\",
\"INTROTEXT\":\"wir bedanken uns f\\u00fcr Ihre Bestellung bei Amazon (Bestellnummer 306-9638137-3397961). Der Zahlungsbetrag wurde bereits entrichtet.\",
\"PAID_DATE\":\"2018-01-02 00:00:00\",
\"IS_CANCELED\":\"0\",
\"INVOICE_DATE\":\"2018-01-02\",
\"DUE_DATE\":\"0000-00-00 00:00:00\",
\"PAYMENT_INFO\":\"01\\\/02\\\/2018 26,
97 \\u20ac () \",
\"PAYMENTS\":[{
\"PAYMENT_ID\":\"7640244\",
\"DATE\":\"01\\\/02\\\/2018\",
\"AMOUNT\":\"26.97\",
\"CURRENCY_CODE\":\"EUR\",
\"NOTE\":\"\",
\"TYPE\":\"\"
}],
\"LASTUPDATE\":\"2018-01-02 17:29:21\",
\"DOCUMENT_URL\":\"https:\\\/\\\/my.fastbill.com\\\/download\\\/DD0fDZvD1CUfB2S4TvLy8GgCuGNKqfbuiNLmn6pa895-yK0E6.20FV3zbC9EhWyC\"
}]
}
}"
(The backslashes seems to appear only in the debugger, so isn't this the problem.)
If I left the response exactly as this, the runtime doesn't give any error, but the JsonConvert.DeserializeObject doesn't return the data from the response in the "fields" of the C# class, otherwise, if I remove the REQUEST and the RESPONSE header and leave only the INVOICES items, seems to "work", but now a new error appears from the compiler:
'Unexpected character encountered while parsing value: {. Path 'VAT_ITEMS', line 1, position 373.'
The C# class:
public class GET_INVOICE {
public string INVOICE_ID { get; set; }
public string TYPE { get; set; }
public string CUSTOMER_ID { get; set; }
public string CUSTOMER_NUMBER { get; set; }
public string CUSTOMER_COSTCENTER_ID { get; set; }
public string CONTACT_ID { get; set; }
public string PROJECT_ID { get; set; }
public string CURRENCY_CODE { get; set; }
public string DELIVERY_DATE { get; set; }
public string INVOICE_TITLE { get; set; }
public string CASH_DISCOUNT_PERCENT { get; set; }
public string CASH_DISCOUNT_DAYS { get; set; }
public string SUB_TOTAL { get; set; }
public string VAT_TOTAL { get; set; }
public IList<string> VAT_ITEMS { get; set; }
public IList<string> ITEMS { get; set; }
public double TOTAL { get; set; }
public string ORGANIZATION { get; set; }
public string NOTE { get; set; }
public string SALUTATION { get; set; }
public string FIRST_NAME { get; set; }
public string LAST_NAME { get; set; }
public string ADDRESS { get; set; }
public string ADDRESS_2 { get; set; }
public string ZIPCODE { get; set; }
public string CITY { get; set; }
public string PAYMENT_TYPE { get; set; }
public string BANK_NAME { get; set; }
public string BANK_ACCOUNT_NUMBER { get; set; }
public string BANK_CODE { get; set; }
public string BANK_ACCOUNT_OWNER { get; set; }
public string BANK_IBAN { get; set; }
public string BANK_BIC { get; set; }
public string COUNTRY_CODE { get; set; }
public string VAT_ID { get; set; }
public string TEMPLATE_ID { get; set; }
public string INVOICE_NUMBER { get; set; }
public string INTROTEXT { get; set; }
public string PAID_DATE { get; set; }
public string IS_CANCELED { get; set; }
public string INVOICE_DATE { get; set; }
public string DUE_DATE { get; set; }
public string PAYMENT_INFO { get; set; }
public IList<string> PAYMENTS { get; set; }
public string LASTUPDATE { get; set; }
public string DOCUMENT_URL { get; set; }
}
The deserialization
JSON.GET_INVOICE GetInvoiceStruct = JsonConvert.DeserializeObject<JSON.GET_INVOICE>(_response);
I really don't understand how I can successfully deserialize the response.
You've successfully prepared model for main object you are deserializing.
Now you also need to model additional models for your collections:
VAT_ITEMS
ITEMS
PAYMENTS
Currently you've modeled them as string but they are not strings at all, they are complex objects.
I'll provide you with example for VatItem and then you can complete the rest yourself.
Create VatItem model, ie:
public class VatItem
{
//Did not used decimal type because I don't know how JSON.NET handles it
public double VAT_PERCENT { get; set; }
public double COMPLETE_NET { get; set; }
public double VAT_VALUE { get; set; }
}
to model this part of json response:
\"VAT_PERCENT\":\"19.00\",
\"COMPLETE_NET\":22.66,
\"VAT_VALUE\":4.3061344545
Then change in GET_INVOICE model your VAT_ITEMS collection to be:
public IList<VatItem> VAT_ITEMS { get; set; }
Follow the example with two additional collections to complete mapping.
My jsonResponse is something like this:
{"status":200,"data":{"first_name":"\u062e\u0633","last_name":"\u0635\u062f\u0627","national_code":"1","image_photo":"1.jpg","cellphone":"1234","city":{"id":1,"name":"x","created_at":"2017-02-27 17:54:44","updated_at":"2017-02-27 17:54:44"},"email":"something#gmail.com","even_odd":1,"Register Time":"2018-01-25 10:39:17","is_blocked":false,"receive_regular_offer":"false","level":1,"ride_count":0,"service_type":1,"bank":"\u0645","iban":"xy","card_number":"","holder":"\u062e\u0633","plate_number":"123","vehicle_model":"\u067e\u0698","vehicle_color":"\u062a\u0627\u06a9\u0633","unique_id":592875}}
I have created a class like this:
public class Driver
{
public string first_name { get; set; }
public string last_name { get; set; }
public string national_code { get; set; }
public string image_photo { get; set; }
public string cellphone { get; set; }
public string city { get; set; }
public string email { get; set; }
public string even_odd { get; set; }
public bool is_blocked { get; set; }
public bool receive_regular_offer { get; set; }
public string level { get; set; }
public string ride_count { get; set; }
public string service_type { get; set; }
public string bank { get; set; }
public string iban { get; set; }
public string card_number { get; set; }
public string holder { get; set; }
public string vehicle_model { get; set; }
public string vehicle_color { get; set; }
public string unique_id { get; set; }
}
and used this:
jsonResponse = reader.ReadToEnd();
JavaScriptSerializer js = new JavaScriptSerializer();
Driver snappDriver = js.Deserialize<Driver>(jsonResponse);
But the result is all null!
1.your class should be defined right
example:
void Main()
{
var json =api();
//dynamic
var dynamic_json = JsonConvert.DeserializeObject(json).Dump() as JObject;
//strong type
var strong_Type_json = JsonConvert.DeserializeObject<Driver>(json).Dump() ;
}
string api(){
return #"
{""status"":200,""data"":{""first_name"":""\u062e\u0633"",""last_name"":""\u0635\u062f\u0627"",""national_code"":""1"",""image_photo"":""1.jpg"",""cellphone"":""1234"",""city"":{""id"":1,""name"":""x"",""created_at"":""2017-02-27 17:54:44"",""updated_at"":""2017-02-27 17:54:44""},""email"":""something#gmail.com"",""even_odd"":1,""Register_Time"":""2018-01-25 10:39:17"",""is_blocked"":false,""receive_regular_offer"":""false"",""level"":1,""ride_count"":0,""service_type"":1,""bank"":""\u0645"",""iban"":""xy"",""card_number"":"""",""holder"":""\u062e\u0633"",""plate_number"":""123"",""vehicle_model"":""\u067e\u0698"",""vehicle_color"":""\u062a\u0627\u06a9\u0633"",""unique_id"":592875}}
";
}
public class City
{
public int id { get; set; }
public string name { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
}
public class Data
{
public string first_name { get; set; }
public string last_name { get; set; }
public string national_code { get; set; }
public string image_photo { get; set; }
public string cellphone { get; set; }
public City city { get; set; }
public string email { get; set; }
public int even_odd { get; set; }
public string Register_Time { get; set; }
public bool is_blocked { get; set; }
public string receive_regular_offer { get; set; }
public int level { get; set; }
public int ride_count { get; set; }
public int service_type { get; set; }
public string bank { get; set; }
public string iban { get; set; }
public string card_number { get; set; }
public string holder { get; set; }
public string plate_number { get; set; }
public string vehicle_model { get; set; }
public string vehicle_color { get; set; }
public int unique_id { get; set; }
}
public class Driver
{
public int status { get; set; }
public Data data { get; set; }
}
2.json's key Register Time in strong type is invalid name
you can add _ in your json string to solve the problem
Try to create exactly the same class definition as your json has. Your Driver class describes json object's data property.
public class DriverWrapper
{
public string status { get; set; }
public Driver data { get; set; }
}
You have to create another object class that handles status and data property
try this
Create a class Response
class Response{
public int status { get; set;}
public Driver data { get; set;}
}
and do it this way
jsonResponse = reader.ReadToEnd();
JavaScriptSerializer js = new JavaScriptSerializer();
Response snappDriver = js.Deserialize<Response>(jsonResponse);
I want to parse a JSON data dynamically and read its key and their values without serializing it in a concrete class because the JSON data can be different time to time. But for the initial testing one sample data is like this . In this data I want to get property values and if the JSON data aslo contains complex structure I also want to read child property values also How to do that in C#.
{"id":4877891717,"email":"rvp#insync.com","accepts_marketing":true,"created_at":"2017-03-24T08:39:56+01:00","updated_at":"2017-04-10T09:40:42+02:00","first_name":"Robin","last_name":"Van Persie","orders_count":8,"state":"disabled","total_spent":"2320.00","last_order_id":4434634693,"note":"","verified_email":true,"multipass_identifier":null,"tax_exempt":true,"phone":"+3225551212","tags":"","last_order_name":"#1116","addresses":[{"id":5143111941,"first_name":"Robin","last_name":"Van Persie","company":"InSync","address1":"CB 28, El Solo Road","address2":"CB 28, El Solo Road","city":"Brussels","province":"EU","country":"Belgium","zip":"123456","phone":"12345678","name":"Robin Van Persie","province_code":null,"country_code":"BE","country_name":"Belgium","default":true}],"default_address":{"id":5143111941,"first_name":"Robin","last_name":"Van Persie","company":"InSync","address1":"CB 28, El Solo Road","address2":"CB 28, El Solo Road","city":"Brussels","province":"EU","country":"Belgium","zip":"123456","phone":"12345678","name":"Robin Van Persie","province_code":null,"country_code":"BE","country_name":"Belgium","default":true}}
I am trying this way. But after geiing tiken what I will do.
foreach (JObject token in jObject.Children())
{}
Thanks
You can use NewtonsoftJson library to parse without creating concrete class
dynamic parseJson = JsonConvert.DeserializeObject("your json");
and get the value using below code like...
string Id=parseJson.Id.Value
I have tested it and it's working for me
you have two options(as far as i know). you can generate a class acording to your json string then parse the result in it. like this:
public class Address
{
public long id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string company { get; set; }
public string address1 { get; set; }
public string address2 { get; set; }
public string city { get; set; }
public string province { get; set; }
public string country { get; set; }
public string zip { get; set; }
public string phone { get; set; }
public string name { get; set; }
public object province_code { get; set; }
public string country_code { get; set; }
public string country_name { get; set; }
public bool #default { get; set; }
}
public class DefaultAddress
{
public long id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string company { get; set; }
public string address1 { get; set; }
public string address2 { get; set; }
public string city { get; set; }
public string province { get; set; }
public string country { get; set; }
public string zip { get; set; }
public string phone { get; set; }
public string name { get; set; }
public object province_code { get; set; }
public string country_code { get; set; }
public string country_name { get; set; }
public bool #default { get; set; }
}
public class RootObject
{
public long id { get; set; }
public string email { get; set; }
public bool accepts_marketing { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public int orders_count { get; set; }
public string state { get; set; }
public string total_spent { get; set; }
public long last_order_id { get; set; }
public string note { get; set; }
public bool verified_email { get; set; }
public object multipass_identifier { get; set; }
public bool tax_exempt { get; set; }
public string phone { get; set; }
public string tags { get; set; }
public string last_order_name { get; set; }
public List<Address> addresses { get; set; }
public DefaultAddress default_address { get; set; }
}
then parse it(using json.net) like this:
var jObject=JsonConvert.DeserializeObject<RootObject>(responseString);
or you can use dynamic object and deal with json result at runtime. like this:
var jObject=JsonConvert.DeserializeObject(responseString);
foreach (JObject token in jObject.Children)
You can use NewtonsoftJson library to parse data easily
dynamic x = Newtonsoft.Json.JsonConvert.DeserializeObject(Jsondata);
foreach (var product in x) {
Messagebox.show(product.data.ToString());
}
I am new to C#. I have successfully consumed the Mailchip API with a simple console app. I am pulling in relevant data and I have successfully used a JSON deserializer on the returned data (Newtonsoft.Json via NuGet). What I need to find is the best way to place the data into variables which I can then pass to a stored procedure to save the data. I know SQL and I know how to pass variables to a stored procedure in C# (connection strings and such).
What I don't know is how to parse through the deserialized data and then place each value into a variable or something I can pass to SQL. I know this is a vague question so here is the current code.
Class MailChimpAPIClient.cs:
//Declare the API Key
public const string APIKey = "My API Key Here";
public static void APIGet(string url)
{
//Syncronious Consumption
var syncClient = new WebClient();
syncClient.Headers.Add(APIKey);
var content = syncClient.DownloadString(url);
//Deseralize the Json String
MailChimpData data = JsonConvert.DeserializeObject<MailChimpData>(content);
}
Class: MailChimpData.cs:
[DataContract]
public class MailChimpData
{
[DataMember]
public List<Unsubscribes> unsubscribes { get; set; }
[DataMember]
public string campaign_id { get; set; }
[DataMember]
public List<Link2> _links { get; set; }
[DataMember]
public int total_items { get; set; }
[DataMember]
public Link link { get; set; }
[DataMember]
public MergeFields mergefields { get; set; }
[DataMember]
public Stats stats { get; set; }
[DataMember]
public Location location { get; set; }
[DataMember]
public List<Member> members { get; set; }
[DataMember]
public string list_id { get; set; }
}
//Unsubscribe Data
public class Link
{
public string rel { get; set; }
public string href { get; set; }
public string method { get; set; }
public string targetSchema { get; set; }
public string schema { get; set; }
}
public class Unsubscribes
{
public string email_id { get; set; }
public string email_address { get; set; }
public string timestamp { get; set; }
public string reason { get; set; }
public string campaign_id { get; set; }
public string list_id { get; set; }
public List<Link> _links { get; set; }
}
public class Link2
{
public string rel { get; set; }
public string href { get; set; }
public string method { get; set; }
public string targetSchema { get; set; }
public string schema { get; set; }
}
//List Data
public class MergeFields
{
public string FNAME { get; set; }
public string LNAME { get; set; }
}
public class Stats
{
public double avg_open_rate { get; set; }
public int avg_click_rate { get; set; }
}
public class Location
{
public double latitude { get; set; }
public double longitude { get; set; }
public int gmtoff { get; set; }
public int dstoff { get; set; }
public string country_code { get; set; }
public string timezone { get; set; }
}
public class Member
{
public string id { get; set; }
public string email_address { get; set; }
public string unique_email_id { get; set; }
public string email_type { get; set; }
public string status { get; set; }
public MergeFields merge_fields { get; set; }
public Stats stats { get; set; }
public string ip_signup { get; set; }
public string timestamp_signup { get; set; }
public string ip_opt { get; set; }
public string timestamp_opt { get; set; }
public int member_rating { get; set; }
public string last_changed { get; set; }
public string language { get; set; }
public bool vip { get; set; }
public string email_client { get; set; }
public Location location { get; set; }
public string list_id { get; set; }
public List<Link> _links { get; set; }
}
Program.cs:
class Program
{
static void Main(string[] args)
{
MailChimpApiClient.APIGet("Custom URL Here");
}
}
So when I run this and look in the Autos window of VS2013 I see this:
members: Count = 4 System.Collections.Generic.List
[0] {MailChimpAPI.Member} MailChimpAPI.Member
_links: Count = 8 System.Collections.Generic.List
email_address: email address here string
email_client: string
email_type: html string
id: (ID Here) string
ip_opt: (IP Here) string
ip_signup: string
language: string
last_changed: 9/16/15 19:31 string
list_id: (List ID Here) string
location: {MailChimpAPI.Location} MailChimpAPI.Location
member_rating: 3 int
merge_fields {MailChimpAPI.MergeFields} MailChimpAPI.MergeFields
stats: {MailChimpAPI.Stats} MailChimpAPI.Stats
status: unsubscribed string
timestamp_opt: 9/16/15 19:26 string
timestamp_signup: string
unique_email_id: (Unique Email ID Here) string
vip: FALSE bool
[1] {MailChimpAPI.Member}: MailChimpAPI.Member
[2] {MailChimpAPI.Member}: MailChimpAPI.Member
[3] {MailChimpAPI.Member}: MailChimpAPI.Member
Raw View
So for each member (0-3), I need to pull each fields value (Field names in bold) into a variable and save it to a database. I just don't know how to parse through each member.
I know this is a long question for a simple C# problem, but if you can remember way back to the beginning of the post I stated "I am new to C#"
Thanks in advance.
I'm getting the following exception when using this bit of code to deserialize a JSON response from CrunchBase. The weird thing is it only happens to certain pages that are being deserialized even though both the results that work fine and the ones that don't both have empty [], empty"", and null values in key:value pairs. How can I cast or correct my mistake?
Exception gets thrown here:
JsonSerializer serializer = new JsonSerializer();
RootObject ro = JsonConvert.DeserializeObject<RootObject>(response);
The inner exception is:
InnerException:
Message=Could not cast or convert from {null} to System.Int32.
Source=Newtonsoft.Json
Thanks for your eyes in advance!
Update:
as asked for the structure of the root object and the additional objects on that JSON endpoint. These were generated by http://json2csharp.com/ after putting the URL of the JSON endpoint into it.
The JSON is long so here are two example links: this one works without error http://api.crunchbase.com/v/1/company/kiip.js , while this other (and others) throws the exception http://api.crunchbase.com/v/1/company/tata-communications.js
public class Image
{
public List<List<object>> available_sizes { get; set; }
public object attribution { get; set; }
}
public class Person
{
public string first_name { get; set; }
public string last_name { get; set; }
public string permalink { get; set; }
}
public class Relationship
{
public bool is_past { get; set; }
public string title { get; set; }
public Person person { get; set; }
}
public class Provider
{
public string name { get; set; }
public string permalink { get; set; }
}
public class Providership
{
public string title { get; set; }
public bool is_past { get; set; }
public Provider provider { get; set; }
}
public class FinancialOrg
{
public string name { get; set; }
public string permalink { get; set; }
}
public class Person2
{
public string first_name { get; set; }
public string last_name { get; set; }
public string permalink { get; set; }
}
public class Investment
{
public object company { get; set; }
public FinancialOrg financial_org { get; set; }
public Person2 person { get; set; }
}
public class FundingRound
{
public string round_code { get; set; }
public string source_url { get; set; }
public string source_description { get; set; }
public double raised_amount { get; set; }
public string raised_currency_code { get; set; }
public int funded_year { get; set; }
public int funded_month { get; set; }
public int funded_day { get; set; }
public List<Investment> investments { get; set; }
}
public class Office
{
public string description { get; set; }
public string address1 { get; set; }
public string address2 { get; set; }
public string zip_code { get; set; }
public string city { get; set; }
public string state_code { get; set; }
public string country_code { get; set; }
public object latitude { get; set; }
public object longitude { get; set; }
}
public class VideoEmbed
{
public string embed_code { get; set; }
public string description { get; set; }
}
public class Screenshot
{
public List<List<object>> available_sizes { get; set; }
public object attribution { get; set; }
}
public class RootObject
{
public string name { get; set; }
public string permalink { get; set; }
public string crunchbase_url { get; set; }
public string homepage_url { get; set; }
public string blog_url { get; set; }
public string blog_feed_url { get; set; }
public string twitter_username { get; set; }
public string category_code { get; set; }
public int number_of_employees { get; set; }
public int founded_year { get; set; }
public int founded_month { get; set; }
public object founded_day { get; set; }
public object deadpooled_year { get; set; }
public object deadpooled_month { get; set; }
public object deadpooled_day { get; set; }
public object deadpooled_url { get; set; }
public string tag_list { get; set; }
public string alias_list { get; set; }
public string email_address { get; set; }
public string phone_number { get; set; }
public string description { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public string overview { get; set; }
public Image image { get; set; }
public List<object> products { get; set; }
public List<Relationship> relationships { get; set; }
public List<object> competitions { get; set; }
public List<Providership> providerships { get; set; }
public string total_money_raised { get; set; }
public List<FundingRound> funding_rounds { get; set; }
public List<object> investments { get; set; }
public object acquisition { get; set; }
public List<object> acquisitions { get; set; }
public List<Office> offices { get; set; }
public List<object> milestones { get; set; }
public object ipo { get; set; }
public List<VideoEmbed> video_embeds { get; set; }
public List<Screenshot> screenshots { get; set; }
public List<object> external_links { get; set; }
}
Json.NET supports JSON Schema. You could create a schema with all the required properties marked and validate incoming JSON against it before deserializing. In this you can check if value is null you can make change it to some default value.
Hope this works for you.