-1

I have hard time deserializing the json response from Solr search engine.

I use Newtonsoft Json.NET.

Simplified response from Solr:

{
   "suggestions": [
      {
         "someword": {
            "numFound": 1
         }
      }
   ]
}

I deserialize this to a list of KeyValuePair<string, customobject>.

customobject test = Newtonsoft.Json.JsonConvert.DeserializeObject<customobject>(jsonText);

It worked, until a user typed the word "key", so Solr returned that JSON:

{
   "suggestions": [
      {
         "key": {
            "numFound": 1
         }
      }
   ]
}

The deserialization of this JSON raise this error :

Unexpected character encountered while parsing value

... Ok. I just realized that even if it doesn't raise an exception with words other than "key", it doesn't work because the KeyValuePair in the output object are always empty.

I had never realized this problem before because this part of the Json (Spellcheck Suggestions) is never used.

So easy fix: we do not serialize suggestions anymore.

But, if anyone still want to answer the question, in what kind of object will you deserialize this Json (from Solr)?

{
    "suggestions":
    [
      {"brack":{
        "numFound":10,
        "startOffset":0,
        "endOffset":5,
        "origFreq":0,
        "suggestion":[{
            "word":"back",
            "freq":78},
          {
            "word":"black",
            "freq":1}
          ]}},
      {"key":{
        "numFound":1,
        "startOffset":6,
        "endOffset":9,
        "origFreq":12,
        "suggestion":[{
            "word":"key",
            "freq":15}]}}]
}

I don't think a dictionary can do the job :

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,Test.SpellCheckSuggestion]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

9
  • What if you try to deserialize it to a Dictionary<string, customobject>? Dictionary should be a collection of key/value pairs with unique keys Commented Aug 23, 2019 at 14:09
  • Please include the code for CustomObject and the code you're using to deserialize. Commented Aug 23, 2019 at 14:54
  • 1
    @Dan can you post a more complete example, including your CustomObject? Commented Aug 23, 2019 at 17:05
  • I deserialize this to a list of KeyValuePair<string, customobject> - then please edit your question to provide a minimal reproducible example that demonstrates the problem by including your data model. See How to Ask. public List<Dictionary<string, Result>> Suggestions { get; set; } works fine for all JSON shown in your question, see dotnetfiddle.net/e8uEiC so I can't reproduce your issue. Commented Aug 23, 2019 at 19:05
  • 1
    @dbc - Thanks for your help! I marked this question as answered. Commented Aug 27, 2019 at 12:24

1 Answer 1

0

As mentioned in a comment above, the solution is to use a list of dictionary. (not just a dictionary and not a list of KeyValuePair)

List<Dictionary<string, SpellCheckSuggestion>>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.