Hi Vikash21,
Refer the below code.
C#
protected void Page_Load(object sender, EventArgs e)
{
StreamReader stRead = new StreamReader(Server.MapPath("~/Json.txt"));
DataTable dataTable = GetDataTableFromJsonString(stRead.ReadToEnd());
gvJsonToDataTable.DataSource = dataTable;
gvJsonToDataTable.DataBind();
}
public DataTable GetDataTableFromJsonString(string json)
{
var jsonLinq = JObject.Parse(json);
// Find the first array using Linq
var srcArray = jsonLinq.Descendants().Where(d => d is JArray).First();
var trgArray = new JArray();
foreach (JObject row in srcArray.Children<JObject>())
{
var cleanRow = new JObject();
foreach (JProperty column in row.Properties())
{
// Only include JValue types
if (column.Value is JValue)
{
cleanRow.Add(column.Name, column.Value);
}
}
trgArray.Add(cleanRow);
}
return JsonConvert.DeserializeObject<DataTable>(trgArray.ToString());
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim stRead As New StreamReader(Server.MapPath("~/Json.txt"))
Dim dataTable As DataTable = GetDataTableFromJsonString(stRead.ReadToEnd())
gvJsonToDataTable.DataSource = dataTable
gvJsonToDataTable.DataBind()
End Sub
Public Function GetDataTableFromJsonString(json As String) As DataTable
Dim jsonLinq = JObject.Parse(json)
' Find the first array using Linq
Dim srcArray = jsonLinq.Descendants().Where(Function(d) TypeOf d Is JArray).First()
Dim trgArray = New JArray()
For Each row As JObject In srcArray.Children(Of JObject)()
Dim cleanRow = New JObject()
For Each column As JProperty In row.Properties()
' Only include JValue types
If TypeOf column.Value Is JValue Then
cleanRow.Add(column.Name, column.Value)
End If
Next
trgArray.Add(cleanRow)
Next
Return JsonConvert.DeserializeObject(Of DataTable)(trgArray.ToString())
End Function
Json.txt
{
"isSuccess": true,
"serviceData":{
"quality_measures":[
{
"measure_name": "string",
"measure_description": "string",
"quality_id": "string",
"emeasure_id": "string",
"emeasure_nqf": "string",
"nqf": "string",
"nqs_domain": "string",
"primary_measure_steward": "string",
"measure_type": "string",
"high_priority": "string",
"claims_submission": "string",
"csv_submission": "string",
"ehr_submission": "string",
"cms_web_interface_submission": "string",
"registry_submission": "string",
"specialty_measure_set": "string"
}
]
}
}