我得到了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"
},
{
"ID":3,
"FeedBackDetail":"Email :sujatakullarkar@gmail.com Mobile :9503507124 Category :Product Request Message :lookingFor",
"CreateDate":"2015-09-04T18:06:44"
}
]
}
如何将其转换为数据表
答案 0 :(得分:0)
需要来自Json.Net
的dll引用using Newtonsoft.Json;
var table = JsonConvert.DeserializeObject<DataTable>(jsonString);
答案 1 :(得分:0)
我想您正在尝试将objFeedBackManagmentViewModel
模型转换为DataTable
。正确?
因此,如果您按照@Plutonix建议的那样关注this帖子,您将得到答案。
总结:
1)首先,使用适当的类反序列化JSON
:
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)然后将@Pravin Pawar优雅地提供的方法放在static class
内的帖子中,如下所示:
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)调用方法:)
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
答案 2 :(得分:-3)
我不知道如何使用Json
你可以试试
foreach(element ele in JsonNodeList)
{
}
我只有xml经验
我也找到了这个,它可能对你有帮助 Convert JSON to DataTable