有一个字符串问题,其中JToken的值在调试中始终为null,但值显示在监视窗口中。
这是转换代码
JToken obj = ((Newtonsoft.Json.Linq.JObject)JsonConvert.DeserializeObject(responseText))["Message"];
var value = obj.ToString();
值总是在null之间进行,但是如果你查看我的观察窗口的屏幕截图,你会发现它有价值。
正在使用的实际数据:
responseText =
"{\"ClassName\":\"System.Data.SqlClient.SqlException\",\"Message\":\"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)\",\"Data\":{\"HelpLink.ProdName\":\"Microsoft SQL Server\",\"HelpLink.EvtSrc\":\"MSSQLServer\",\"HelpLink.EvtID\":\"-1\",\"HelpLink.BaseHelpUrl\":\"http://go.microsoft.com/fwlink\",\"HelpLink.LinkId\":\"20476\",\"SqlError 1\":\"System.Data.SqlClient.SqlError: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)\"},\"InnerException\":null,\"HelpURL\":null,\"StackTraceString\":\" at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling)\\r\\n at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)\\r\\n at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)\\r\\n at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)\\r\\n at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)\\r\\n at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)\\r\\n at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)\\r\\n at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)\\r\\n at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)\\r\\n at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)\\r\\n at System.Data.SqlClient.SqlConnection.Open()\\r\\n at MieTrakDotNetCoreAPI.Utilities.DaoUtil.ExecuteStoredProcedure(String procedureName, String[] parameters) in E:\\\\Projects\\\\MieTrak2018\\\\CustomerCustomizations\\\\MieTrakDotNetCoreAPI\\\\MieTrakDotNetCoreAPI\\\\Utilities\\\\DaoUtil.cs:line 111\",\"RemoteStackTraceString\":null,\"RemoteStackIndex\":0,\"ExceptionMethod\":null,\"HResult\":-2146232060,\"Source\":\"Core .Net SqlClient Data Provider\",\"WatsonBuckets\":null,\"Errors\":null,\"ClientConnectionId\":\"00000000-0000-0000-0000-000000000000\"}"
答案 0 :(得分:2)
尝试使用SelectToken
获取数据。
例如:
JToken obj = ((Newtonsoft.Json.Linq.JObject)JsonConvert.DeserializeObject(responseText));
var value = obj.SelectToken("Message").ToString();