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());
}
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.
i am trying to convert JSON to object following is my json
{"entity":"event","account_id":"acc_8yTsyb2WJOlcka","event":"payment.captured","contains":["payment"],"payload":{"payment":{"entity":{"id":"pay_AKR45WLH0g1ANu","entity":"payment","amount":100,"currency":"INR","status":"captured","order_id":"order_AKR41LsWIgOAB1","invoice_id":null,"international":false,"method":"netbanking","amount_refunded":0,"refund_status":null,"captured":true,"description":"Admission Fees","card_id":null,"bank":"SBIN","wallet":null,"vpa":null,"email":"xxxxx.xxxx#xxx.xxx","contact":"xxxxx","notes":{"address":"NA","merchant_order_id":"2516"},"fee":2,"tax":0,"error_code":null,"error_description":null,"created_at":1528367383}}},"created_at":1528367384}
and the code I am trying to convert to object is
jsonString = JsonConvert.SerializeObject(documentContents);
RazorPayPayload desJsonString = JsonConvert.DeserializeObject<RazorPayPayload>(jsonString);
and the classes where I want to deserialized
public class RazorPayPayload
{
public string entity { get; set; }
public string account_id { get; set; }
public string events { get; set; }
public List<string> contains { get; set; }
public payments payload { get; set; }
public string created_at { get; set; }
}
public class payments
{
public Entities payment { get; set; }
}
public class notes
{
public string address { get; set; }
public string merchant_order_id { get; set; }
public string source { get; set; }
}
public class Entities
{
public Entity entity { get; set; }
}
public class Entity
{
public string id { get; set; }
public string entity { get; set; }
public string amount { get; set; }
public string currency { get; set; }
public string order_id { get; set; }
public string invoice_id { get; set; }
public string international { get; set; }
public string method { get; set; }
public string amount_refunded { get; set; }
public string refund_status { get; set; }
public string captured { get; set; }
public string description { get; set; }
public string card_id { get; set; }
public string bank { get; set; }
public string wallet { get; set; }
public string vpa { get; set; }
public string email { get; set; }
public string contact { get; set; }
public notes notes { get; set; }
public string fee { get; set; }
public string tax { get; set; }
public string error_code { get; set; }
public string error_description { get; set; }
}
I am getting the error "Error converting value to type 'FeePayr_Razor_Webhook.RazorPayPayload'"
Have you tried changing:
{"entity":"event","account_id":"acc_8yTsyb2WJOlcka","event":
to
{"entity":"event","account_id":"acc_8yTsyb2WJOlcka","events":
That would better match your class definition. Or rename your class field to event.
There is a useful visual studio function for converting a json object to c# classes.
Copy the JSON into the clipboard. (helps if the JSON object properties holds data, to determine the data type)
Go to visual studio and place cursor where you'd like to paste in the c# classes.
click edit => paste special => paste json as classes
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 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.
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());