1

I have a class which is converting from JSON text

This is my code

   SearchResult myojb = (SearchResult)js.Deserialize(objText, typeof(SearchResult));

And the SearchResult class is

 public class SearchResult
    {
        public List<Datum> data { get; set; }
        public int page { get; set; }
        public int per_page { get; set; }
        public int total_count { get; set; }
        public string search_id { get; set; }
    }

And DAtum class is

public class Datum
    {
        public string id { get; set; }
        public string description { get; set; }
        public string added_date { get; set; }
        public string media_type { get; set; }
        public Contributor contributor { get; set; }
        public string aspect { get; set; }
        public string image_type { get; set; }
        public bool is_editorial { get; set; }
        public bool is_adult { get; set; }
        public bool is_illustration { get; set; }
        public bool has_model_release { get; set; }
        public bool has_property_release { get; set; }
        public List<string> releases { get; set; }
        public List<Category> categories { get; set; }
        public List<string> keywords { get; set; }
        public Assets assets { get; set; }
        public List<Model> models { get; set; }
    }

I want to convert the List data of SearchResult class into datatable. for conversion I am using https://stackoverflow.com/a/18100872 answer. But not understand how to call it with List data.

Please help stuck with this issue.

Community
  • 1
  • 1
Gitz
  • 810
  • 1
  • 17
  • 48
  • The link you provided is exactly what you need. Can you show the code you have so far and where you have trouble? Hint: Microsoft recommends uppercase for property name – DerApe Jul 23 '15 at 10:25
  • You have a method that accepts any kind of generic lists like `List`. So you just need to pass it as argument and you get the `DataTable`. What's the problem? – Tim Schmelter Jul 23 '15 at 10:27
  • @TimSchmelter Sir I did't understand how to call it with `myojb.data ` or what ? – Gitz Jul 23 '15 at 10:28
  • @Gitz: do you want a `DataTable` which contains only the rows which are in the `List` or do you want one which also contains all properties of `SearchResult`? If the former: `DataTable result = ToDataTable(myobj.data)` – Tim Schmelter Jul 23 '15 at 10:31
  • @TimSchmelter Sir i only want a `Datatable` which contains only the rows which are in `List – Gitz Jul 23 '15 at 10:34
  • 1
    ToDataTable(myojb.data); – Neel Jul 23 '15 at 10:35
  • @Neel Hi !! neel i need your help if you have time? – Gitz Jul 23 '15 at 10:54
  • hey, yeah say what is the matter @Gitz – Neel Jul 23 '15 at 10:57
  • @Neel I am able to create the `datatable` now but inside the `Datum` class some more `list` and I am not able to get this data into `datatable` rows .. – Gitz Jul 23 '15 at 11:01
  • @Neel Do you able to understand the issue which I am getting ? – Gitz Jul 23 '15 at 11:09
  • yes i was looking for that but could not reproduce as can i know what exactly "I am not able to get this data" mean i mean what is happening? – Neel Jul 23 '15 at 11:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84073/discussion-between-gitz-and-neel). – Gitz Jul 23 '15 at 11:27
  • hey @Gitz sorry cant use chat during office it is blocked – Neel Jul 23 '15 at 11:29
  • hi, have you got the solution? – Neel Jul 24 '15 at 04:16
  • @Neel No dear not yet stuck with this issue actually ... – Gitz Jul 24 '15 at 04:17

3 Answers3

2

i only want a Datatable which contains only the rows which are in List<Datum>

Since you already have the method it's simple:

DataTable tblDatum = ToDataTable(myobj.data)
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

If you already got the searchresult with data from json. As per your Stackoverflow link. The code for converting your list to datatable is as follows:

public static DataTable ToDataTable<T>(List<T> items)
        {
            DataTable dataTable = new DataTable(typeof(T).Name);

            //Get all the properties
            PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo prop in Props)
            {
                //Setting column names as Property names
                dataTable.Columns.Add(prop.Name);
            }
            foreach (T item in items)
            {
                var values = new object[Props.Length];
                for (int i = 0; i < Props.Length; i++)
                {
                    //inserting property values to datatable rows
                    values[i] = Props[i].GetValue(item, null);
                }
                dataTable.Rows.Add(values);
            }
            //put a breakpoint here and check datatable
            return dataTable;
        }

Use will be as follows:

SearchResult myobj = (SearchResult)js.Deserialize(objText, typeof(SearchResult));
            if (myobj.data != null && myobj.data.Count > 0)
            {
                DataTable dt = ToDataTable(myobj.data);
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow item in dt.Rows)
                    {
                        foreach (DataColumn dc in dt.Columns)
                        {
                            Console.WriteLine(item[dc]);
                        }

                    }
                }
            }
0
YourList.CreateDataTable("NewNameForYourDataTable");

by the way name for your DataTable is optional so you can write this as well :

YourList.CreateDataTable();