Alex In Paris
Alex In Paris

Reputation: 1077

Cannot deserialize the current JSON object..into type 'System.Collections.Generic.List`1[System.String]'

Full error

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[System.String]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'CEC1B0C9-A87A-4A9E-A017-AEFC3CEDCBD4', line 2, position 42.

Before this bit of code, there's one that works well...

var highlighting = JsonConvert.DeserializeObject<Object>(jsonDeserialized["highlighting"].ToString());

This gives us some data from our search engine:

{\r\n  "C6C1B0C9-A87A-4A9E-A017-AEFC3CEDCBD4": {\r\n    "Title_fr":
[\r\n      "<em>Chris</em> <em>Cep</em>"\r\n    ]\r\n  } ,\r\n 
"874BE5B1-FAA0-463F-98DD-8532D4D80178": {\r\n    "Content_fr": [\r\n  
" <em>Chris</em> <em>Cep</em>, Ctifl \n\n9 Info - Tgif /
January - February 2001\n\nThe Tgif on the Net\nInquire"\r\n   
]\r\n  } ,\r\n  "44FA5B99-9472-499C-9827-646A511068DA": {\r\n   
"Content_fr": [\r\n      " S\n\nHorti\nThe
database\ndocument\n<em>Chris</em> <em>Cep</em>, Iso
Rever,\nJeane Samuel"\r\n    ]\r\n  } \r\n}

//formatted - http://jsonblob.com/53d24a78e4b0ed12c5a6b1df

{
  "C6C1B0C9-A87A-4A9E-A017-AEFC3CEDCBD4": {
    "Title_fr": [
      "<em>Chris</em> <em>Cep</em>"
    ]
  },
  "874BE5B1-FAA0-463F-98DD-8532D4D80178": {
    "Content_fr": [
      " <em>Chris</em> <em>Cep</em>, Ctifl \n\n9 Info - Tgif /    January - February 2001\n\nThe Tgif on the Net\nInquire"
    ]
  },
  "44FA5B99-9472-499C-9827-646A511068DA": {
    "Content_fr": [
      " S\n\nHorti\nThe database\ndocument\n<em>Chris</em> <em>Cep</em>, Iso Rever,\nJeane Samuel"
    ]
  }
}

The problem, I believe, is that the three objects below are all "named" differently. I.e. I can't do JsonConvert.DeserializeObject<List<string>>(jsonDeserialized["highlighting"]["CAN'T-REFERENCE-ANYTHING-HERE"].ToString()) because the names of those nodes are GUIDs like C6C1B0C9-A87A-4A9E-A017-AEFC3CEDCBD4.

I'm still relatively green with JSON - what am I missing here? How could I get the three objects in "highlighting" into a List<> of some sort?

My solution:

    private class Highlighters //defined elsewhere
    {
        public string IdGlobal;
        public string FieldName;
        public string FieldValue;
    }

    var highlighters = new List<Highlighters>();
    var allHighlights = JsonConvert.DeserializeObject<IDictionary<string, object>>(jsonDeserialized["highlighting"].ToString());
    foreach (var entry in allHighlights)
    {
        string guid = entry.Key;
        string j = entry.Value.ToString();
        var insideEntry = JsonConvert.DeserializeObject<IDictionary<string, object>>(j);

        foreach (var h in insideEntry)
        {
            string highlightedField = h.Key;
            string highlightedValue = h.Value.ToString().Substring(1, h.Value.ToString().LastIndexOf("]") - 1).Trim().Trim('"').Trim();//a little cleaning

            highlighters.Add(new Highlighters{IdGlobal=guid, FieldName = highlightedField, FieldValue = highlightedValue});
        }
    }

Upvotes: 0

Views: 1678

Answers (1)

cynic
cynic

Reputation: 5405

If you're not interested in the keys (which are GUIDs in your example), deserialize the JSON to a Dictionary<string, JObject> and access the .Values of the resulting dictionary.

Upvotes: 1

Related Questions