Asked by:
Not getting values of Object in Json array

Question
-
User339833461 posted
Hello Everyone,
I am facing problem with reading object values into Json array. please help me what is the problem here. Below is my code. I am sending employee list to get Json array. but i am not getting 'EmployeeDetails' in json array(that is in second level).what is the problem here?
below is my code
class Program { static void Main(string[] args) { List<Employee> list = new List<Employee>(); Employee emp = new Employee { ID = 101, Department = "Stocks", EmployeeDetails = new Name { FirstName = "S", LastName = "Charles", Email = "abc@gmail.com" } }; Employee emp1 = new Employee { ID = 102, Department = "Stores", EmployeeDetails = new Name { FirstName = "L", LastName = "Dennis", Email = "Den@gmail.com" } }; list.Add(emp); list.Add(emp1); var resul1t = Program.GetEmployeeDetails(list); } private static string GetEmployeeDetails(List<Employee> emp) { string jsonarray = ""; if ((emp != null) && (emp.Count > 0)) { Dictionary<string, object> dic = new Dictionary<string, object>(); int i = 0; foreach (var awo in emp) { dic.Add(i.ToString(), ObjectToString(awo)); i++; } if (dic.Count > 0) { jsonarray = DictionnaryToArray(dic); } } return jsonarray; } private static string ObjectToString(object obj) { Type objType = obj.GetType(); IList<PropertyInfo> props = new List<PropertyInfo>(objType.GetProperties()); StringBuilder sb = new StringBuilder(1024); foreach (PropertyInfo prop in props) { var type = prop.GetValue(obj, null); string attributeValueString = string.Format("\"{0}\":\"{1}\"", prop.Name, prop.GetValue(obj, null)); if (type != null && type.GetType() == typeof(double)) { var doubleToStringValue = Convert.ToString(prop.GetValue(obj, null), System.Globalization.CultureInfo.InvariantCulture); attributeValueString = string.Format("\"{0}\":\"{1:0.0}\"", prop.Name, doubleToStringValue); } sb.Append(attributeValueString).Append(";"); } return "{" + sb.ToString().TrimEnd(new char[] { ';' }) + "}"; } private static string DictionnaryToArray(Dictionary<string, object> data) { return "{" + string.Join(";", (from c in data select string.Format("\"{0}\":{1}", c.Key.ToString(), c.Value.ToString())).ToArray()) + "}"; } } public class Employee { public int? ID { get; set; } public string Department { get; set; } public Name EmployeeDetails { get; set; } } public class Name { public string LastName { get; set; } public string Email { get; set; } public string FirstName { get; set; } }
Thanks
Thursday, April 23, 2020 10:30 AM
All replies
-
User475983607 posted
This thread is like you other where you have not explained the problem you are trying to solve. Below is the results of your code which is clearly not correct. Can you share the JSON results you expect given the input?
{ "0": { "ID": "\"101\";\"Department\"" null, : "\"Stocks\";\"EmployeeDetails\"" null, : "CsConsole.Name" } ";\"1\"", : { "ID": "\"102\";\"Department\"" null, : "\"Stores\";\"EmployeeDetails\"" null, : "CsConsole.Name" } }
Are you trying to create a dictionary like below?
{ "0": { "ID": 101, "Department": "Stocks", "EmployeeDetails": { "LastName": "Charles", "Email": "abc@gmail.com", "FirstName": "S" } }, "1": { "ID": 102, "Department": "Stores", "EmployeeDetails": { "LastName": "Dennis", "Email": "Den@gmail.com", "FirstName": "L" } } }
static void Main(string[] args) { List<Employee> list = new List<Employee>() { new Employee { ID = 101, Department = "Stocks", EmployeeDetails = new Name { FirstName = "S", LastName = "Charles", Email = "abc@gmail.com" } }, new Employee { ID = 102, Department = "Stores", EmployeeDetails = new Name { FirstName = "L", LastName = "Dennis", Email = "Den@gmail.com" } } }; int i = 0; var dic = list.ToDictionary(x => i++, x => x); string json = JsonConvert.SerializeObject(dic, Formatting.Indented); Console.WriteLine(json); }
Friday, April 24, 2020 1:52 PM -
User288213138 posted
Hi Learning Rocks,
I am facing problem with reading object values into Json array. please help me what is the problem here. Below is my code. I am sending employee list to get Json array. but i am not getting 'EmployeeDetails' in json array(that is in second level).If you want to convernt class to json, you can try to use the JsonConvert.SerializeObject() method.
List<Employee> list = new List<Employee>(); Employee emp = new Employee { ID = 101, Department = "Stocks", EmployeeDetails = new Name { FirstName = "S", LastName = "Charles", Email = "abc@gmail.com" } }; Employee emp1 = new Employee { ID = 102, Department = "Stores", EmployeeDetails = new Name { FirstName = "L", LastName = "Dennis", Email = "Den@gmail.com" } }; list.Add(emp); list.Add(emp1); var resul1t = JsonConvert.SerializeObject(list)
Console.WriteLine(resul1t);The result:
[{"ID":101,"Department":"Stocks","EmployeeDetails{"LastName":"Charles","Email":"abc@gmail.com","FirstName":"S"}}, {"ID":102,"Department":"Stores","EmployeeDetails{"LastName":"Dennis","Email":"Den@gmail.com","FirstName":"L"}}]
Best regards,
Sam
Saturday, April 25, 2020 8:24 AM