我有一个应该返回json的Web服务。
我知道我可以使用JsonConvert.DeserializeObject<ClassType>(jsonString)
。但是我无法从字符串创建类,因为我从结构结构未知的sql存储过程中获取了json字符串。它是动态的,每次Web服务调用时,我都会得到另一个结构。
Web服务代码为:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class Dispatcher_Service : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetProcedureDetails(string JSON_IN)
{
try
{
string JSON_OUT = null;
string ReturnValue = null;
JObject json = new JObject();
string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection SqlConn = new SqlConnection(ConnectionString))
{
using (SqlCommand command = new SqlCommand("UWQ.p_Web_Dispatcher", SqlConn))
{
EventLog.WriteEntry("UWQ_DispatcherService", "INPUT: " + JSON_IN);
command.CommandType = CommandType.StoredProcedure;
//SqlDbType.NVarChar,-1 is nvarchar(max)
command.Parameters.Add("@_p_Request_JSON_IN", SqlDbType.NVarChar, -1).Value = JSON_IN;
command.Parameters.Add("@_p_Result_JSON_OUT", SqlDbType.NVarChar, -1);
command.Parameters["@_p_Result_JSON_OUT"].Direction = ParameterDirection.Output;
SqlConn.Open();
command.ExecuteNonQuery();
SqlConn.Close();
JSON_OUT = command.Parameters["@_p_Result_JSON_OUT"].Value.ToString();
}
}
return JSON_OUT;
}
catch (Exception ex)
{
EventLog.WriteEntry("UWQ_DispatcherService", "Error occur in Get Procedure Details from Dispatcher: " + ex.Message);
return null;
}
}
void CreateEventLog()
{
try
{
if (!EventLog.SourceExists("UWQ_DispatcherService"))
{
EventLog.CreateEventSource("UWQ_DispatcherService", "UWQ_DispatcherService");
}
}
catch (Exception exc)
{
using (EventLog eventLog = new EventLog("Application"))
{
eventLog.Source = "Application";
eventLog.WriteEntry("Can't Create source log massage:" + exc.Message, EventLogEntryType.Information, 101, 1);
}
}
}
}
I have tried to use with Jobject.parse but it add extra braces to the json.
任何想法?