sangeetha k

sangeetha k

  • 1.9k
  • 207
  • 36.3k

How to serialise and deserialise a list in c#

Jul 13 2018 4:22 AM
Borrower result = new Borrower();
List<Borrower> brwlist = new List<Borrower>();
brwlist = new List<Borrower> {
new Borrower
{
ID = 210626,
BorrowerStatus = "",
BrwAdd1 = "Property Address line 1",
BrwAdd2 = "",
BrwCellPhoneNo = "",
Brwcity = "Palmbay",
BrwEmaillID = "",
BrwHomePhNo = "",
Brwstate = "AK",
BrwZipCode = "89874",
DateOfBirth = null,
deduction = null,
FirstName = "John",
income = null,
LastName = "Smith",
LoanID = 89191,
PropertyInfo = null,
Sequence = 1
}
};
string dres = JsonConvert.SerializeObject(brwlist,Formatting.Indented);
var brw = JsonConvert.DeserializeObject<List<Borrower>>(dres.ToString());
HttpContext.Current.Session["BorrowerList"] = brwlist;
// var b = brwlist.ConvertAll(x => Convert.ToString(x));
var res = _BorrowerController.BorrowerAddress() as JsonResult;
//string a = brwlist.ToString()
//string a = Newtonsoft.Json.JsonConvert.SerializeObject(new Borrower
//{
// ID = 210626,
// BorrowerStatus = "",
// BrwAdd1 = "Property Address line 1",
// BrwAdd2 = "",
// BrwCellPhoneNo = "",
// Brwcity = "Palmbay",
// BrwEmaillID = "",
// BrwHomePhNo = "",
// Brwstate = "AK",
// BrwZipCode = "89874",
// DateOfBirth = null,
// deduction = null,
// FirstName = "John",
// income = null,
// LastName = "Smith",
// LoanID = 89191,
// PropertyInfo = null,
// Sequence = 1
//});
// var dres = JsonConvert.DeserializeObject<Borrower>(a);
string jresult = JsonConvert.SerializeObject(res.Data, Formatting.Indented);
var dresult = JsonConvert.DeserializeObject<List<Borrower>>(jresult.ToString());
// var dresult1 = JsonConvert.DeserializeObject(a.ToString());
Assert.AreEqual(dresult,brw);
// Assert.AreEqual(dresult, (dres));
// Assert.AreEqual(dresult.BorrowerStatus, dresult.BorrowerStatus);
}

Answers (3)