0

I got the json result as follows.

{  
   "HTTPStatusCode":"200",
   "objFeedBackManagmentViewModel":[  
      {  
         "ID":2,
         "FeedBackDetail":"Email :trupti.undirwade@gandhibagh.com Mobile :9503628985 Category :Product Request Message :lookingFor",
         "CreateDate":"2015-09-04T13:42:45"
      },
      {  
         "ID":3,
         "FeedBackDetail":"Email :sujatakullarkar@gmail.com Mobile :9503507124 Category :Product Request Message :lookingFor",
         "CreateDate":"2015-09-04T18:06:44"
      }
   ]
}

how to convert it into datatable

Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69
  • Possible duplicate of [How to convert json into datatable?](http://stackoverflow.com/questions/7641004/how-to-convert-json-into-datatable) Also: http://stackoverflow.com/q/11981282/1070452 – Ňɏssa Pøngjǣrdenlarp Sep 13 '16 at 13:42
  • Not a strict duplication as this JSON would need a child table to take the objects in 'objFeedBackManagmentViewModel'. You'd need a DataSet surely? – matt_lethargic Sep 13 '16 at 13:44

3 Answers3

0

Require dll refernce from Json.Net

using Newtonsoft.Json;
var table = JsonConvert.DeserializeObject<DataTable>(jsonString);
Shamseer K
  • 4,964
  • 2
  • 25
  • 43
0

I guess that you are trying to convert the objFeedBackManagmentViewModel model into a DataTable. Right?

So, if you follow this post as @Plutonix suggested, You'll get the answer.

In summary:

1) First, Deserialize the JSON using some appropriate classes:

public class OKStatus{
    public int HTTPStatusCode {get; set;}
    public FeedBack[] objFeedBackManagmentViewModel {get; set;}
}

public class FeedBack{
    public int ID {get; set;}
    public string FeedBackDetail {get; set;}
    public DateTime CreateDate {get; set;}      
}

2) Then put the method that @Pravin Pawar gracefully provided in the post inside a static class like this:

public static class ExtensionMethods{
    public static DataTable ToDataTable<T>(this IList<T> data)
    {
        PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));

        DataTable table = new DataTable();

        for(int i = 0 ; i < props.Count ; i++)
        {
            PropertyDescriptor prop = props[i];
            table.Columns.Add(prop.Name, prop.PropertyType);
        }

        object[] values = new object[props.Count];

        foreach (T item in data)
        {
            for (int i = 0; i < values.Length; i++)
            {
                values[i] = props[i].GetValue(item);
            }

            table.Rows.Add(values);
        }

        return table;        
    }
}

3) Call the method :)

var json = "{\"HTTPStatusCode\":\"200\",\"objFeedBackManagmentViewModel\":[{\"ID\":2,\"FeedBackDetail\":\"Email :trupti.undirwade@gandhibagh.com Mobile :9503628985 Category :Product Request Message :lookingFor\",\"CreateDate\":\"2015-09-04T13:42:45\"}]}";

var result = JsonConvert.DeserializeObject<OKStatus>(json);

var table = result.objFeedBackManagmentViewModel.ToDataTable(); 

//table contains the datatable of the feedback model
Community
  • 1
  • 1
Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69
-3

i have no idea how to work with Json

you could try a

foreach(element ele in JsonNodeList)
{
}

i only have xml experience though

i also found this one, it might help you Convert JSON to DataTable

Community
  • 1
  • 1