首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >错误“无法将当前JSON对象反序列化为类型,因为该类型需要JSON数组才能正确反序列化”

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

Stack Overflow用户
提问于 2018-06-01 06:56:55
回答 1查看 449关注 0票数 0

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

测试:

代码语言:javascript
复制
    [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

代码语言:javascript
复制
  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.
EN

回答 1

Stack Overflow用户

发布于 2018-06-01 07:05:20

如果你看一下你粘贴的json,你会发现它是错误的:

代码语言:javascript
复制
  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'
        }
      }
    ]";
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50633910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档