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

Alex In Paris

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});
        }
    }
cynic

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List

From Dev

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`

From Dev

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List

From Dev

JSON.Net - cannot deserialize the current json object (e.g. {"name":"value"}) into type 'system.collections.generic.list`1

From Dev

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1'

From Dev

Object not storing JSON data: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List

From Dev

Converting JSON to Object fails - Cannot deserialize the current JSON object into System.Collections.Generic.List

From Dev

C# JSON error => Cannot deserialize JSON object into type 'System.Collections.Generic.IEnumerable`1

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<string>'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.DataRow>' to 'System.Collections.Generic.List<string>'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<String>' to 'System.Collections.Generic.IEnumerable<turon.Model.Products_Products>

From Dev

Cannot convert type 'System.Collections.Generic.List<string>' to 'System.Web.Mvc.SelectList'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type:>>' to 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type System.Collections.Generic.List<> to System.Collections.Generic.List<>

From Dev

Unable to cast object of type 'System.Collections.Generic.List`1 [<>f__AnonymousType6`65[System.String,System.Decimal,System.Nullable`1

From Dev

Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'System.Collections.Generic.List`

From Dev

Cannot deserialize the current JSON object - Null List

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >'

From Dev

Cannot implicitly convert type 'void' to 'System.Collections.Generic.List<string>'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<MODEL#1>' to 'System.Collections.Generic.List<Model#2>

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List

From Dev

How to handle type “Object {System.Collections.Generic.List<object>}”

From Dev

Cannot convert from 'string' to 'system.collections.generic.list string'

From Dev

Cannot implicitly convert type to 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type System.Collections.Generic.List

From Dev

Cannot implicitly convert anonymous type to System.Collections.Generic.List

Related Related

  1. 1

    Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List

  2. 2

    Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`

  3. 3

    Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List

  4. 4

    JSON.Net - cannot deserialize the current json object (e.g. {"name":"value"}) into type 'system.collections.generic.list`1

  5. 5

    Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1'

  6. 6

    Object not storing JSON data: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List

  7. 7

    Converting JSON to Object fails - Cannot deserialize the current JSON object into System.Collections.Generic.List

  8. 8

    C# JSON error => Cannot deserialize JSON object into type 'System.Collections.Generic.IEnumerable`1

  9. 9

    Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'

  10. 10

    Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<string>'

  11. 11

    Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.DataRow>' to 'System.Collections.Generic.List<string>'

  12. 12

    Cannot implicitly convert type 'System.Collections.Generic.List<String>' to 'System.Collections.Generic.IEnumerable<turon.Model.Products_Products>

  13. 13

    Cannot convert type 'System.Collections.Generic.List<string>' to 'System.Web.Mvc.SelectList'

  14. 14

    Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type:>>' to 'System.Collections.Generic.List

  15. 15

    Cannot implicitly convert type System.Collections.Generic.List<> to System.Collections.Generic.List<>

  16. 16

    Unable to cast object of type 'System.Collections.Generic.List`1 [<>f__AnonymousType6`65[System.String,System.Decimal,System.Nullable`1

  17. 17

    Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'System.Collections.Generic.List`

  18. 18

    Cannot deserialize the current JSON object - Null List

  19. 19

    Cannot implicitly convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >'

  20. 20

    Cannot implicitly convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >'

  21. 21

    Cannot implicitly convert type 'void' to 'System.Collections.Generic.List<string>'

  22. 22

    Cannot implicitly convert type 'System.Collections.Generic.List<MODEL#1>' to 'System.Collections.Generic.List<Model#2>

  23. 23

    Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List

  24. 24

    How to handle type “Object {System.Collections.Generic.List<object>}”

  25. 25

    Cannot convert from 'string' to 'system.collections.generic.list string'

  26. 26

    Cannot implicitly convert type to 'System.Collections.Generic.List

  27. 27

    Cannot implicitly convert type 'System.Collections.Generic.List

  28. 28

    Cannot implicitly convert type System.Collections.Generic.List

  29. 29

    Cannot implicitly convert anonymous type to System.Collections.Generic.List

HotTag

Archive