How can I serialize and deserialize a type with a string member that contains “raw” JSON, without escaping the JSON in the process

后端 未结 3 1490
梦谈多话
梦谈多话 2020-11-30 13:10

I want to Deserialize JSON into Object but I don\'t want to Deserialize nested JSON, nested of nested JSON should convert into JSON list (Please check \"My expected

3条回答
  •  情深已故
    2020-11-30 13:48

    I think you will have to rebuild your list of Employees :

            RootObject Employees = JsonConvert.DeserializeObject(jsonEmployees);
    
            List EmployeesNew = new List();
    
            foreach (var item in Employees.Employees)
            {
                string StringAddress = JsonConvert.SerializeObject(item.Address, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
                EmployeesNew.Add(new Employee { EmpId = item.EmpId, EmpName = item.EmpName, AddressString = StringAddress });
    
            }
    

    Your class:

        public class Employee
        {
            public int EmpId { get; set; }
            public string EmpName { get; set; }
            // **Note** : I'm not using List
    data type for Address, instead of I want list of address in JSON string public List Address { get; set; } public string AddressString { get; set; } } public class RootObject { public List Employees { get; set; } } public class AddressItems { public int AddressId { get; set; } public string Address { get; set; } }

提交回复
热议问题