反序列化CouchBase响应时,JsonConvert.DeserializeObject引发异常

时间:2020-01-17 20:08:09

标签: c# json asp.net-core json.net couchbase

我正在尝试反序列化来自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": "[]", ...}}

1 个答案:

答案 0 :(得分:1)

根据问题末尾发布的字符串结果,可能是row已经是JObject。根据{{​​3}}的帖子,似乎在反序列化之前将.ToString()添加到jobject可能对您有用。

例如:

string rowString = row.ToString();
var registrationDetails = JsonConvert.DeserializeObject<IEnumerable<RegistrationModel>>(rowString);