[英]JsonConvert.DeserializeObject throws exception when deserializing CouchBase response
I'm trying to deserialize JSON response from CouchBase.我正在尝试反序列化来自 CouchBase 的 JSON 响应。 However, deserialization throws an exception.但是,反序列化会引发异常。
IQueryRequest queryRequest = QueryRequest.Create(queryString);
queryRequest.ScanConsistency(ScanConsistency.RequestPlus);
var queryResult = await bucket.QueryAsync<dynamic>(queryRequest);
if (!queryResult.Success)
{
}
foreach (var row in queryResult.Rows)
{
try
{
var registrationDetails = JsonConvert.DeserializeObject<IEnumerable<RegistrationModel>>(row);
//var registrationDetail1 = JsonConvert.DeserializeObject<RegistrationModel>(row);
}
catch (Exception Ex)
{ }
}
Exception I'm getting:我得到的例外:
{Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The best overloaded method match for 'Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.IEnumerable<MC4B_CommonInterface.RegistrationModel>>(string)' has some invalid arguments
at CallSite.Target(Closure , CallSite , Type , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
at DataAccess.Services.RegistrationDataServices.ActivateSimpleRegistrations(ActivateRequest request) ...}
Response from Couchbase is straightforward. Couchbase 的回应很直接。 RegistrationModel
has all the JsonProperty
attributes. RegistrationModel
具有所有JsonProperty
属性。
[
{
"acsPntDocId": "",
"adLis": "[]",
"adP2DNa": "CA",
.....
}
]
One thing I've noticed, is the foreach (var row in queryResult.Rows)
, row
contains {{"acsPntDocId":"","adLis": "[]", ...}}
我注意到的一件事是foreach (var row in queryResult.Rows)
, row
包含{{"acsPntDocId":"","adLis": "[]", ...}}
Based on the string result you posted at the end of the question, it could be that row
is already be a JObject.根据您在问题末尾发布的字符串结果,该row
可能已经是 JObject。 Based on this post https://stackoverflow.com/a/44308752/579148 it seems that adding .ToString()
to the jobject before deserializing it may do the trick for you.基于这篇文章https://stackoverflow.com/a/44308752/579148似乎在反序列化之前将.ToString()
添加到 jobject 可能对你有用。
For example:例如:
string rowString = row.ToString();
var registrationDetails = JsonConvert.DeserializeObject<IEnumerable<RegistrationModel>>(rowString);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.