locked
Deserialize Json Object RRS feed

  • Question

  • User1587720337 posted

    I am trying to deserialize the following json string:

    "{\"city\":\"hh\"},{\"city\":\"Honolulu\"},{\"city\":\"Honolulu\"},{\"city\":\"Aiea\"},{\"city\":\"Honolulu\"},{\"city\":\"Honolulu\"},{\"city\":\"Honolulu\"},{\"city\":\"Honolulu\"},{\"city\":\"Honolulu\"},{\"city\":\"Ewa Beach\"}"

    C# Code:

    var test2 = "{\"city\":\"hh\"},{\"city\":\"Honolulu\"},{\"city\":\"Honolulu\"},{\"city\":\"Aiea\"},{\"city\":\"Honolulu\"},{\"city\":\"Honolulu\"},{\"city\":\"Honolulu\"},{\"city\":\"Honolulu\"},{\"city\":\"Honolulu\"},{\"city\":\"Ewa Beach\"}";
    //IEnumerable<PropertyResult> result = JsonConvert.DeserializeObject<IEnumerable<PropertyResult>>(test2);
    var objects = JsonConvert.DeserializeObject<List<PropertyResult>>(test2);
    items = new string[] { objects[0].city, objects[1].city };

    Error Message:

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

    Any ideas, how to get this work.

    Wednesday, January 4, 2017 2:47 AM

Answers

  • User-573138384 posted

    As it rightly said, its expecting an array when you deserialize it to collection. An array is represented by square brackets in json format. You missed it.

    var test2 = "[{\"city\":\"hh\"},{\"city\":\"Honolulu\"},{\"city\":\"Honolulu\"},{\"city\":\"Aiea\"},{\"city\":\"Honolulu\"},{\"city\":\"Honolulu\"},{\"city\":\"Honolulu\"},{\"city\":\"Honolulu\"},{\"city\":\"Honolulu\"},{\"city\":\"Ewa Beach\"}]";

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 4, 2017 1:10 PM

All replies

  • User-573138384 posted

    As it rightly said, its expecting an array when you deserialize it to collection. An array is represented by square brackets in json format. You missed it.

    var test2 = "[{\"city\":\"hh\"},{\"city\":\"Honolulu\"},{\"city\":\"Honolulu\"},{\"city\":\"Aiea\"},{\"city\":\"Honolulu\"},{\"city\":\"Honolulu\"},{\"city\":\"Honolulu\"},{\"city\":\"Honolulu\"},{\"city\":\"Honolulu\"},{\"city\":\"Ewa Beach\"}]";

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 4, 2017 1:10 PM
  • User753101303 posted

    Hi,

    Also make sure you need to do that (you serialized this string yourself as well which might explain it is not a valid array?). ASP.NET allows to handle serialization/deserialization for you so I'm always a bit suspicious ;-) when I see someone trying to explicitely serialize/deserialize  a string in its own code.

    In particular a pattern I see quite often is to see someone that does the serialization from an object to a string, then having ASP.NET serializing this string and having it deserialized implicitely and then again explicitely on the client side.

    It works but basically you have each time an extra serialization/deserialization which is most often not needed.

    Wednesday, January 4, 2017 1:40 PM