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

C# “错误”;无法将当前JSON对象反序列化为类型,因为该类型需要JSON数组才能正确反序列化";,c#,arrays,json,serialization,json.net,C#,Arrays,Json,Serialization,Json.net,当我尝试将JSON字符串反序列化到类列表中时,我得到一个“无法将当前JSON对象反序列化为类型,因为该类型需要一个JSON数组才能正确反序列化”。由于上述错误,我无法成功反序列化数据。 任何帮助都将不胜感激 测试: [Fact] public void TestDeserializeStringIntoObject() { var jsonString =@"[ { 'reviewId':'

当我尝试将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; }        
  }
[事实]
public void testDeserializeStringInObject()的
{
var jsonString=@“[{
“reviewId”:“201805312334145341”,
“提供者”:[
{
“providerId”:“1245383488”,
“PCP许可证编号”:“ABC123”,
“PCP_名称”:“ramu shamu”,
“专业学位”:“医学博士”,
“产品代码”:“10”
},
{
“providerId”:“1245383488”,
“PCP许可证编号”:“BCD123”,
“PCP_名称”:“champa chameli”,
“专业学位”:“医学博士”,
“产品代码”:“10”
}
]
},
{
“reviewId”:“201805312334145341”,
“提供者”:{
“providerId”:“1073527099”,
“PCP许可证编号”:“CDE123”,
“PCP_名称”:“爸爸爸爸爸爸”,
“专业学位”:“医学博士”,
“产品代码”:“10”
}
}
]";
var reviewhistores=JsonConvert.DeserializeObject(jsonString);
}
公开课复习
{
公共字符串reviewId{get;set;}
[JsonProperty(“提供商”)]
公共列表提供程序{get;set;}
}
公共类提供者
{
[JsonProperty(“providerId”)]
公共字符串providerId{get;set;}
[JsonProperty(“PCP许可证号”)]
公共字符串PCP_许可证号{get;set;}
[JsonProperty(“PCP_名称”)]
公共字符串PCP_名称{get;set;}
[JsonProperty(“profDegree”)]
公共字符串{get;set;}
[JsonProperty(“productCode”)]
公共字符串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.
无法将当前JSON对象(例如{“name”:“value”})反序列化为类型“System.Collections.Generic.List`1[LACare.SchedulingEngine.Model.Provider]”,因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。
若要修复此错误,请将JSON更改为JSON数组(例如[1,2,3]),或更改反序列化类型,使其成为可以从JSON对象反序列化的正常.NET类型(例如,不是integer之类的基元类型,也不是数组或列表之类的集合类型)。还可以将JsonObjectAttribute添加到类型中,以强制它从JSON对象反序列化。
路径'[1].Providers.providerId',第25行,位置19。

如果查看粘贴的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'
        }
      }
    ]";
var jsonString=@“[{
“reviewId”:“201805312334145341”,

“Providers”:[数组上的第一个对象有Providers数组,而第二个对象有Providers对象。downvoter可以解释他们为什么进行了DownVote吗?您没有显示完整的反序列化代码谢谢@nikosi。我忽略了这部分。@ps2goat:谢谢,我已经添加了代码。