Arvind Singh

Arvind Singh

  • 307
  • 5.7k
  • 775k

Convert c# object to json & deserialize back json to c# obje

Jul 14 2016 2:57 AM
i need to create json string like this 
"DoiPerson": {
"originId": "1000009230",
"listId": "ORG_GLOBAL/ORG_BH/CUS",
"legalStatus": "N",
"lastName": "SUNDARESAN",
"firstName": "SUJAI",
"fullName": "SUJAI SUNDARESAN",
"sex": "M",
"function": "CEO",
"profOccupation": "1292156",
"dob": "03/12/1970 00:00:00.00000"
so i create class
public class DoiPerson
{
public string originId { get; set; }
public string listId { get; set; }
public string legalStatus { get; set; }
public string lastName { get; set; }
public string firstName { get; set; }
public string fullName { get; set; }
public string sex { get; set; }
public string function { get; set; }
public string profOccupation { get; set; }
public string dob { get; set; }
using this i am able to create proper json string 
string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(personObj); 
i am sending that string to some API and getting response as json string in below format. 
 "DoiPerson": {
"originId": "1000009230",
"listId": "ORG_GLOBAL/ORG_BH/CUS",
"globalScore": "20"
}
i want to deserialize it and assign this to class object using below code:
DoiPerson deserialized = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<DoiPerson>(jsonstring); 
 
but my DoiPerson class does not has  globalScore property so it is not converting properly and if i add globalScore property to my class it is converting json string into my object properly
BUT at the time of  creattion of json fom class object it also add that property shown below:
"DoiPerson": {
"globalScore" :=null, 
"originId": "1000009230",
"listId": "ORG_GLOBAL/ORG_BH/CUS",
"legalStatus": "N",
"lastName": "SUNDARESAN",
"firstName": "SUJAI",
"fullName": "SUJAI SUNDARESAN",
"sex": "M",
"function": "CEO",
"profOccupation": "1292156",
"dob": "03/12/1970 00:00:00.00000"
}
 but at the time of json creation i dont want that property 
please suggest any solution.

Answers (2)