0

I am getting a "cannot deserialize the current JSON object into type because the type requires a JSON array to deserialize correctly" when I try to deserialize a json string into a class list. I am not able to successfully deserialize the data due to the above error. Any help would be much appreciated!

Test:

    [Fact]
        public void TestDeserializeStringIntoObject()
            {
              var jsonString =@"[  {
              'reviewId':'201805312334145341', 
            'Providers': [
                    {
                        'providerId': '1245383488',
                        'PCP_License_No': 'ABC123',
                        'PCP_Name': 'ramu shamu',
                        'profDegree': 'MD',
                        'productCode': '10'
                    },
                    {
                        'providerId': '1245383488',
                        'PCP_License_No': 'BCD123',
                        'PCP_Name': 'champa chameli',
                        'profDegree': 'MD',
                        'productCode': '10'
                    }
                ]
          },
          {
              'reviewId':'201805312334145341', 
            'Providers': {
              'providerId': '1073527099',
              'PCP_License_No': 'CDE123',
              'PCP_Name': 'baba baba',
              'profDegree': 'MD',
              'productCode': '10'
            }
          }
        ]";
 var reviewHistories = JsonConvert.DeserializeObject<List<ReviewHistory>>(jsonString);
}

public class ReviewHistory
  {
    public string reviewId { get; set; }
    [JsonProperty("Providers")]
    public List<Provider> Providers { get; set; }
  }

public class Provider
  {
    [JsonProperty("providerId")]
    public string providerId { get; set; }
    [JsonProperty("PCP_License_No")]
    public string PCP_License_No { get; set; }
    [JsonProperty("PCP_Name")]
    public string PCP_Name { get; set; }
    [JsonProperty("profDegree")]
    public string profDegree { get; set; }
    [JsonProperty("productCode")]
    public string productCode { get; set; }        
  }

StackTrace

  Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[LACare.SchedulingEngine.Model.Provider]' 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<T>) 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 '[1].Providers.providerId', line 25, position 19.
Ajit Goel
  • 4,180
  • 7
  • 59
  • 107

1 Answers1

1

If you look at the json you pasted, you will see it is malformed:

  var jsonString =@"[  {
          'reviewId':'201805312334145341', 
        'Providers': [ <----- this is an array
                {
                    'providerId': '1245383488',
                    'PCP_License_No': 'ABC123',
                    'PCP_Name': 'ramu shamu',
                    'profDegree': 'MD',
                    'productCode': '10'
                },
                {
                    'providerId': '1245383488',
                    'PCP_License_No': 'BCD123',
                    'PCP_Name': 'champa chameli',
                    'profDegree': 'MD',
                    'productCode': '10'
                }
            ]
      },
      {
          'reviewId':'201805312334145341', 
        'Providers': { <---- but same property here is an object?
          'providerId': '1073527099',
          'PCP_License_No': 'CDE123',
          'PCP_Name': 'baba baba',
          'profDegree': 'MD',
          'productCode': '10'
        }
      }
    ]";
zaitsman
  • 8,984
  • 6
  • 47
  • 79