serving the solutions day and night

Pages

Friday, April 1, 2016

Convert JSON to Object using Json.NET

sample json file
[{"empNumber":"123456","primaryName":{"firstName":"FN","lastName":"LN"},"disability":false,"otherNames":[{"firstName":"FN","lastName":"LN1"},{"firstName":"FN","lastName":"LN2"}],"homeAddress":{"addressLine1":"1234 Python Java Rd","city":"Bellevue","state":"WI","postalCode":"628204"}},{"empNumber":"7890","primaryName":{"firstName":"1FN","lastName":"1LN"},"disability":true,"otherNames":[{"firstName":"1FN","lastName":"1LN1"},{"firstName":"1FN","lastName":"1LN2"}],"homeAddress":{"addressLine1":"5869 Dotnet CRM St","city":"Chicago","state":"NE","postalCode":"567567"}}]

Download Json.NET dll from http://www.newtonsoft.com/json


Class Objects
public class Names
{
public string firstName { get; set; }
public string lastName { get; set; }
}

public class Address
{
public string addressLine1 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postalCode { get; set; }
}

public class Employee
{
public string empNumber { get; set; }
public Names primaryName { get; set; }
public bool disability { get; set; }
public IList<Names> otherNames { get; set; }
}

Read the JSON file 
string jsondatas = File.ReadAllText(JSONFile);

Convert json datas to Employee object array
Employee[] emps = JsonConvert.DeserializeObject<Employee[]>(jsondatas);

for loop to get each object.
foreach (Employee emp in emps)
{
    //convert object back to json string
    string jsonemp = Newtonsoft.Json.JsonConvert.SerializeObject(emp);
   
    //convert json to object
    Employee emp1 = JsonConvert.DeserializeObject<Employee>(jsonemp);
}


5 comments:

Harini R said...

thanks for sharing wonderful blog with us. its really more helpful to our institute candidates to get aware some useful knowledge keep sharing more information.
Dot Net training in Chennai

keerthi said...

It's like you read my mind! You seem to know a lot about this, like you wrote the book in it or something. I think that you can do with some pics to drive the message home a little bit, but instead of that, this is fantastic blog. A great read. I will definitely be back.

Online Training in Chennai

Sowmiya said...

Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.



Digital Marketing Company in Chennai

Harish Raju said...

This blog explains the details about changing the ways of doing that business. That is understand well and doing some different process. Provides he best output of others. Thanks for this blog.
Web Designing Training in Chennai

Alvinasarah said...

This idea is mind blowing. I think everyone should know such information like you have described on this post. Thank you for sharing this explanation.Your final conclusion was good. We are sowing seeds and need to be patiently wait till it blossoms.
Tax Accountant Melbourne
Investment Advisor Melbourne
Mortgage Broker
Mortgage Broker Melbourne