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.
CustomObject?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.