简体   繁体   中英

JsonConvert.DeserializeObject throws exception when deserializing CouchBase response

I'm trying to deserialize JSON response from CouchBase. 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. RegistrationModel has all the JsonProperty attributes.

[
  {
    "acsPntDocId": "",
    "adLis": "[]",
    "adP2DNa": "CA",
     .....
    }
]

One thing I've noticed, is the foreach (var row in queryResult.Rows) , row contains {{"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. 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.

For example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM