How to make nested json from json string

后端 未结 1 562
醉梦人生
醉梦人生 2021-01-29 08:23

I am tring to create nested json string here is my json string below,

[  
   {  
      \'CompanyID\':\'1\',
      \'Name\':\'Company1\',
      \'DepartmentName\'         


        
相关标签:
1条回答
  • 2021-01-29 08:45

    One of the cleanest ways of such manipulation is with using classes. Based on the json you initially provided, and your initial question, you can do the following.:

    class Company
    {
    
        public int CompanyID { get; set; }
        public string Name { get; set; }
        public string DepartmentName { get; set; }
    
        public object Modify => new {
            CompanyID, Name, department = new { DepartmentName }
        };
    
    }
    

    Then you can simply deserialize into a list, convert it with the help of Linq and serialize it

    var modifiedList = JsonConvert.DeserializeObject<List<Company>>(jsonCompany).Select(c => c.Modify);
    string updatedJson = JsonConvert.SerializeObject(modifiedList);
    

    Since your code seems to show a different json, you may have to modify the Linq part according to what you are actually trying to do. You can look into Linq to check what exactly you are trying to do.

    0 讨论(0)
提交回复
热议问题