错误“无法将当前JSON对象反序列化为类型,因为该类型需要JSON数组才能正确反序列化”

时间:2018-05-31 22:56:55

标签: c# arrays json serialization json.net

当我尝试将json字符串反序列化为类列表时,我得到“无法将当前JSON对象反序列化为类型,因为该类型需要JSON数组才能正确反序列化”。由于上述错误,我无法成功反序列化数据。 任何帮助将不胜感激!

测试

    [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; }        
  }

栈跟踪

  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.

1 个答案:

答案 0 :(得分:1)

如果你看一下你粘贴的json,你会发现它格式不正确:

  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'
        }
      }
    ]";