You can very easily write web service with JSON data return format.
please check below method, this may help you.
[WebMethod(Description = "Gets Student information")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetStudent()
{
SqlConnection objConnection = new SqlConnection(strconnectionString);
SqlCommand objCommand = new SqlCommand("SELECT * FROM Student", objConnection);
DataSet objDataSet = new DataSet();
SqlDataAdapter objDataAdapter = new SqlDataAdapter(objCommand);
objDataAdapter.Fill(objDataSet, "reading");
objConnection.Close();
// Create a multidimensional jagged array
string[][] JaggedArray = new string[objDataSet.Tables[0].Rows.Count][];
int i = 0;
foreach (DataRow rs in objDataSet.Tables[0].Rows)
{
JaggedArray[i] = new string[] { rs["Name"].ToString(), rs["Roll"].ToString(), rs["Class"].ToString() };
i = i + 1;
}
// Return JSON data
JavaScriptSerializer js = new JavaScriptSerializer();
string strJSON = js.Serialize(JaggedArray);
return strJSON;
}
please check below method, this may help you.
[WebMethod(Description = "Gets Student information")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetStudent()
{
SqlConnection objConnection = new SqlConnection(strconnectionString);
SqlCommand objCommand = new SqlCommand("SELECT * FROM Student", objConnection);
DataSet objDataSet = new DataSet();
SqlDataAdapter objDataAdapter = new SqlDataAdapter(objCommand);
objDataAdapter.Fill(objDataSet, "reading");
objConnection.Close();
// Create a multidimensional jagged array
string[][] JaggedArray = new string[objDataSet.Tables[0].Rows.Count][];
int i = 0;
foreach (DataRow rs in objDataSet.Tables[0].Rows)
{
JaggedArray[i] = new string[] { rs["Name"].ToString(), rs["Roll"].ToString(), rs["Class"].ToString() };
i = i + 1;
}
// Return JSON data
JavaScriptSerializer js = new JavaScriptSerializer();
string strJSON = js.Serialize(JaggedArray);
return strJSON;
}
No comments:
Post a Comment