Convert a data row to a JSON object

后端 未结 4 1711
天涯浪人
天涯浪人 2020-12-11 16:08

I have a DataTable which has only a single row and looks like

   America              |  Africa               |     Japan     |   
   ----------         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-11 16:56

    Like Gazzi said you should include the NewtonSoft.Json library it is included in Visual Studio.

    To get an object similar to what you have described you should create a data model like Country.cs which would look like this.

    [Serializable]
    public class CountryModel {
       public int ID { get; set; }
       public string Title { get; set; }
       public List Values { get; set; }
    }
    
    [Serializable]
    public class ValueModel {
       public int ValueID { get; set; }
       public string Type { get; set; }
    }
    

    Create a helper function to convert your table data to these models. You can then serialize/deserialize data as need using the Newtonsoft Library.

提交回复
热议问题