我正在尝试反序列化来自CouchBase的JSON响应。但是,反序列化引发异常。
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)
{ }
}
我得到的异常:
{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) ...}
来自Couchbase的响应非常简单。 RegistrationModel
具有所有JsonProperty
属性。
[
{
"acsPntDocId": "",
"adLis": "[]",
"adP2DNa": "CA",
.....
}
]
我注意到的一件事是foreach (var row in queryResult.Rows)
,row
包含{{"acsPntDocId":"","adLis": "[]", ...}}
答案 0 :(得分:1)
根据问题末尾发布的字符串结果,可能是row
已经是JObject。根据{{3}}的帖子,似乎在反序列化之前将.ToString()
添加到jobject可能对您有用。
例如:
string rowString = row.ToString();
var registrationDetails = JsonConvert.DeserializeObject<IEnumerable<RegistrationModel>>(rowString);