try
{
string credentials = String.Format("{0}:{1}", "usernameValues", "PasswordValues");
// real key!!!!
byte[] bytes = Encoding.ASCII.GetBytes(credentials);
string base64 = Convert.ToBase64String(bytes);
string authorization = String.Concat("Basic ", base64);
HttpWebRequest GETRequest = (HttpWebRequest)WebRequest.Create("YOUR WebApi URL");
GETRequest.Method = "GET";
GETRequest.Headers.Add("authorization", authorization);
GETRequest.Timeout = 200000;
GETRequest.Accept = "application/json";
GETRequest.ContentType="application/json";
GETRequest.UseDefaultCredentials = true;
GETRequest.PreAuthenticate = true;
GETRequest.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse GETResponse = (HttpWebResponse)GETRequest.GetResponse();
Stream GETResponseStream = GETResponse.GetResponseStream();
StreamReader sr = new StreamReader(GETResponseStream);
string jsonString = sr.ReadToEnd();
DataTable dt = JsonStringToDataTable(jsonString.Replace("\n", "").Replace("null", string.Empty));
}
catch (WebException webex)
{
}
public DataTable JsonStringToDataTable(string jsonString)
{
DataTable dt = new DataTable();
string[] jsonStringArray = Regex.Split(jsonString.Replace("[", "").Replace("]", ""), "},{");
List<string> ColumnsName = new List<string>();
foreach (string jSA in jsonStringArray)
{
string[] jsonStringData = Regex.Split(jSA.Replace("{", "").Replace("}", ""), ",");
foreach (string ColumnsNameData in jsonStringData)
{
try
{
int idx = ColumnsNameData.IndexOf(":");
string ColumnsNameString = ColumnsNameData.Substring(0, idx - 1).Replace("\"", "");
if (!ColumnsName.Contains(ColumnsNameString))
{
ColumnsName.Add(ColumnsNameString);
}
}
catch (Exception ex)
{
throw new Exception(string.Format("Error Parsing Column Name : {0}", ColumnsNameData));
}
}
break;
}
foreach (string AddColumnName in ColumnsName)
{
dt.Columns.Add(AddColumnName.Trim());
}
foreach (string jSA in jsonStringArray)
{
string[] RowData = Regex.Split(jSA.Replace("{", "").Replace("}", ""), ",");
DataRow nr = dt.NewRow();
foreach (string rowData in RowData)
{
try
{
int idx = rowData.IndexOf(":");
string RowColumns = rowData.Substring(0, idx - 1).Replace("\"", "").Trim();
string RowDataString = rowData.Substring(idx + 1).Replace("\"", "");
nr[RowColumns] = RowDataString.Trim();
}
catch (Exception ex)
{
continue;
}
}
dt.Rows.Add(nr);
//foreach (DataRow dr in dt.Rows) // search whole table
//{
// if (dr["MANUAL_ADJUSTMENT"].ToString() == string.Empty) // if id==2
// {
// dr["MANUAL_ADJUSTMENT"] = "0.0000"; //change the name
// //break; break or not depending on you
// }
//}
}
return dt;
}
{
string credentials = String.Format("{0}:{1}", "usernameValues", "PasswordValues");
// real key!!!!
byte[] bytes = Encoding.ASCII.GetBytes(credentials);
string base64 = Convert.ToBase64String(bytes);
string authorization = String.Concat("Basic ", base64);
HttpWebRequest GETRequest = (HttpWebRequest)WebRequest.Create("YOUR WebApi URL");
GETRequest.Method = "GET";
GETRequest.Headers.Add("authorization", authorization);
GETRequest.Timeout = 200000;
GETRequest.Accept = "application/json";
GETRequest.ContentType="application/json";
GETRequest.UseDefaultCredentials = true;
GETRequest.PreAuthenticate = true;
GETRequest.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse GETResponse = (HttpWebResponse)GETRequest.GetResponse();
Stream GETResponseStream = GETResponse.GetResponseStream();
StreamReader sr = new StreamReader(GETResponseStream);
string jsonString = sr.ReadToEnd();
DataTable dt = JsonStringToDataTable(jsonString.Replace("\n", "").Replace("null", string.Empty));
}
catch (WebException webex)
{
}
public DataTable JsonStringToDataTable(string jsonString)
{
DataTable dt = new DataTable();
string[] jsonStringArray = Regex.Split(jsonString.Replace("[", "").Replace("]", ""), "},{");
List<string> ColumnsName = new List<string>();
foreach (string jSA in jsonStringArray)
{
string[] jsonStringData = Regex.Split(jSA.Replace("{", "").Replace("}", ""), ",");
foreach (string ColumnsNameData in jsonStringData)
{
try
{
int idx = ColumnsNameData.IndexOf(":");
string ColumnsNameString = ColumnsNameData.Substring(0, idx - 1).Replace("\"", "");
if (!ColumnsName.Contains(ColumnsNameString))
{
ColumnsName.Add(ColumnsNameString);
}
}
catch (Exception ex)
{
throw new Exception(string.Format("Error Parsing Column Name : {0}", ColumnsNameData));
}
}
break;
}
foreach (string AddColumnName in ColumnsName)
{
dt.Columns.Add(AddColumnName.Trim());
}
foreach (string jSA in jsonStringArray)
{
string[] RowData = Regex.Split(jSA.Replace("{", "").Replace("}", ""), ",");
DataRow nr = dt.NewRow();
foreach (string rowData in RowData)
{
try
{
int idx = rowData.IndexOf(":");
string RowColumns = rowData.Substring(0, idx - 1).Replace("\"", "").Trim();
string RowDataString = rowData.Substring(idx + 1).Replace("\"", "");
nr[RowColumns] = RowDataString.Trim();
}
catch (Exception ex)
{
continue;
}
}
dt.Rows.Add(nr);
//foreach (DataRow dr in dt.Rows) // search whole table
//{
// if (dr["MANUAL_ADJUSTMENT"].ToString() == string.Empty) // if id==2
// {
// dr["MANUAL_ADJUSTMENT"] = "0.0000"; //change the name
// //break; break or not depending on you
// }
//}
}
return dt;
}