oclockvn
4/6/2015 - 7:30 AM

parseJson.cs

void btnParse_Click(object sender, EventArgs e)
{
 string json = string.Empty;

 // read json string from file
 using (StreamReader reader = new StreamReader("employees.json"))
 {
  json = reader.ReadToEnd();
 }

 JavaScriptSerializer jss = new JavaScriptSerializer();
 List<Employee> employees = new List<Employee>();

 // convert json string to dynamic type
 var obj = jss.Deserialize<dynamic>(json);

 // get contents
 int len = obj["employees"].Length;
 for (int i = 0; i &lt; len; i++)
 {
  var employee = new Employee
  {
   FirstName = obj["employees"][i]["firstName"],
   LastName = obj["employees"][i]["lastName"]
  };

  employees.Add(employee);
 }

 // add employees to richtextbox
 foreach (var employee in employees)
 {
  rtbResult.AppendText(string.Format("Fullname: {0} {1}\n", employee.FirstName, employee.LastName));
 }
}