1

It gives me error when deserializing this JSON File

{
  "checkOut": "10:30",
  "stars": 4,
  "locationId": 953,
  "propertyType": 6,
  "checkIn": "15:00",
  "trustyou": {
    "languageSplit": [
      {
        "tripTypeSplit": [
          {
            "type": "family",
            "percentage": 85
          },
          {
            "type": "couple",
            "percentage": 15
          }
        ],
        "name": "de",
        "percentage": 100
      }
    ],
    "location": [

    ],
    "reviewsCount": 83,
    "popularity": 0,
    "tripTypeSplit": [
      {
        "type": "family",
        "percentage": 86
      },
      {
        "type": "couple",
        "percentage": 14
      }
    ],
    "sentimentScoreList": [
      {
        "categoryId": "14",
        "ratio": "Good",
        "shortText": "Great location",
        "name": "Location",
        "subcategories": [

        ],
        "highlights": [
          {
            "text": "Beautiful location",
            "confidence": 100
          }
        ],
        "reviewCount": 14,
        "score": 100
      },
      {
        "categoryId": "111",
        "ratio": "Good",
        "shortText": "Rather comfortable",
        "name": "Comfort",
        "subcategories": [
        ],
        "highlights": [

        ],
        "reviewCount": 5,
        "score": 100
      },

I have the following classes for this JSON

public class Root
    {

        [JsonProperty("checkIn")]
        public string CheckIn { get; set; }

        [JsonProperty("distance")]
        public double Distance { get; set; }

        [JsonProperty("hidden")]
        public bool Hidden { get; set; }

        [JsonProperty("trustyou")]
        public Trustyou Trustyou { get; set; }

        [JsonProperty("amenitiesV2")]
        public AmenitiesV2 AmenitiesV2 { get; set; }

        [JsonProperty("hasAirbnb")]
        public bool HasAirbnb { get; set; }

        [JsonProperty("checkOut")]
        public string CheckOut { get; set; }

        [JsonProperty("popularity")]
        public int Popularity { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("id")]
        public int Id { get; set; }

        [JsonProperty("cntRooms")]
        public int CntRooms { get; set; }

What seems to be the problem? i'm deserializing this using

    string resp2 = await client.GetStringAsync("");
    var hotelDetails = JsonConvert.DeserializeObject<IDictionary<string, HotelsDescriptionAPI.Root>>(resp2, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
    foreach (var hoteldesc in hotelDetails)
    {
        MessageBox.Show(hoteldesc.Value.Id);
    }    

and the exact error is

"Error converting value 24545 to type and  Error converting value "10:30" to type 'HotelsDescriptionAPI.Root'. Path 'checkOut', line 1, position 19."

Im trying to get the value of "Id", What could be the problem with my code?

3 Answers 3

5

Your deserialization code should be:

var hotelDetails = JsonConvert.DeserializeObject<HotelsDescriptionAPI.Root>(resp2, 
                   new JsonSerializerSettings { 
                       NullValueHandling = NullValueHandling.Ignore 
                   });

You're trying to deserialize it into a dictionary of string,Root, when the object itself is simply Root.

Sign up to request clarification or add additional context in comments.

6 Comments

i have tried that im getting "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'HotelsDescriptionAPI.AmenitiesV2' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly."
@Nevi Well yes, look at the JSON: "amenitiesV2": [ ],. It's an array, not an object. Your code should probably be IList<AmenitiesV2> instead of just AmenitiesV2, but it's hard to tell, because the array is empty in the JSON.
i can get the I.d without an error by decorating the amenitiesV2 property with JSONIgnore but sometimes amenitiesV2 has a value depending on JSON data, i will deserialize the RootObject because im gonna get the name and description and id not just only amenities
i have tried [JsonProperty("amenitiesV2", NullValueHandling = NullValueHandling.Ignore)] still getting error
@Nevi Did you try my suggestion? Make it a List of AmenitiesV2.
|
0

It does not seem to apply to your scenario, but note that when you do have JSON that is an array (root level children are array items, not properties), you may have to change the root object to subclass a compatible type.

For example:

public class RootObject : List<ChildObject>
{
}

public class ChildObject
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

Comments

-1

For people that this does not help, and that are not using Entity Framework and or writing the domain classes by hand - make sure all your class properties match what is coming out of your data source in the same exact field order.

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.