无法将当前JSON数组(例如[1,2,3])反序列化为类型

时间:2017-08-03 09:32:06

标签: c# arrays json serialization

我有一个问题要将JSON反序列化为一个对象,我一直在搜索并找到以下2个问题

1:(Cannot deserialize the current JSON array (e.g. [1,2,3]) into type

2:(Additional text encountered after finished reading JSON content:

Json回应

[
  {
    "$id": "1",
    "CompanyNumber": "000000",
    "Address2": null,
    "Address3": null,
    "PostCode": "Some POst Code",
    "County": "UNITED KINGDOM",
    "AddressDescription": null,
    "OutOfBusiness": false,
    "StopDistributionIndicator": null,
    "BranchIndicator": false,
    "TelephoneNumber": "0000000",
    "State": "",
    "ConfidenceCode": 4,
    "Number": "734125938",
    "CompanyName": "Something  LIMITED",
    "Address1": " Lower Road",
    "CountryCode": "GB",
    "Town": "London"
  },
  {
    "$id": "2",
    "CompanyNumber": "000000",
    "Address2": null,
    "Address3": null,
    "PostCode": "Some POst Code",
    "County": "UNITED KINGDOM",
    "AddressDescription": null,
    "OutOfBusiness": false,
    "StopDistributionIndicator": null,
    "BranchIndicator": false,
    "TelephoneNumber": "0000000",
    "State": "",
    "ConfidenceCode": 4,
    "Number": "734125938",
    "CompanyName": "Something  LIMITED",
    "Address1": " Lower Road",
    "CountryCode": "GB",
    "Town": "London"
  },
  {
    "$id": "3",
    "CompanyNumber": "000000",
    "Address2": null,
    "Address3": null,
    "PostCode": "Some POst Code",
    "County": "UNITED KINGDOM",
    "AddressDescription": null,
    "OutOfBusiness": false,
    "StopDistributionIndicator": null,
    "BranchIndicator": false,
    "TelephoneNumber": "0000000",
    "State": "",
    "ConfidenceCode": 4,
    "Number": "734125938",
    "CompanyName": "Something  LIMITED",
    "Address1": " Lower Road",
    "CountryCode": "GB",
    "Town": "London"
  }
]

我的班级看起来像

 [JsonProperty(PropertyName = "$id")]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "CompanyNumber")]
    public string CompanyNumber { get; set; }

    [JsonProperty(PropertyName = "Address2")]
    public object Address2 { get; set; }

    [JsonProperty(PropertyName = "Address3")]
    public object Address3 { get; set; }

    [JsonProperty(PropertyName = "PostCode")]
    public string PostCode { get; set; }

    [JsonProperty(PropertyName = "County")]
    public string County { get; set; }

    [JsonProperty(PropertyName = "AddressDescription")]
    public object AddressDescription { get; set; }

    [JsonProperty(PropertyName = "OutOfBusiness")]
    public bool OutOfBusiness { get; set; }

    [JsonProperty(PropertyName = "StopDistributionIndicator")]
    public object StopDistributionIndicator { get; set; }

    [JsonProperty(PropertyName = "BranchIndicator")]
    public bool BranchIndicator { get; set; }

    [JsonProperty(PropertyName = "TelephoneNumber")]
    public string TelephoneNumber { get; set; }

    [JsonProperty(PropertyName = "State")]
    public string State { get; set; }

    [JsonProperty(PropertyName = "ConfidenceCode")]
    public int ConfidenceCode { get; set; }

    [JsonProperty(PropertyName = "Number")]
    public string Number { get; set; }

    [JsonProperty(PropertyName = "CompanyName")]
    public string CompanyName { get; set; }

    [JsonProperty(PropertyName = "Address1")]
    public string Address1 { get; set; }

    [JsonProperty(PropertyName = "CountryCode")]
    public string CountryCode { get; set; }

    [JsonProperty(PropertyName = "Town")]
    public string Town { get; set; }

当我尝试按照第一个问题(它是从json响应的开头和结尾删除[])时,如果Json只包含有关像

这样的公司的信息,它将起作用
 [
  {
    "$id": "1",
    "CompanyNumber": "000000",
    "Address2": null,
    "Address3": null,
    "PostCode": "Some POst Code",
    "County": "UNITED KINGDOM",
    "AddressDescription": null,
    "OutOfBusiness": false,
    "StopDistributionIndicator": null,
    "BranchIndicator": false,
    "TelephoneNumber": "0000000",
    "State": "",
    "ConfidenceCode": 4,
    "Number": "734125938",
    "CompanyName": "Something  LIMITED",
    "Address1": " Lower Road",
    "CountryCode": "GB",
    "Town": "London"
  }
]

当有更多信息如上面的Json我得到错误 '完成阅读JSON内容后遇到的其他文字: 我正在尝试使用下面的代码去除json;

dynamic x = Newtonsoft.Json.JsonConvert.DeserializeObject<SearchResult>(t2, new Newtonsoft.Json.JsonSerializerSettings() { NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore });

任何帮助都会受到赞赏,因为我被困了

1 个答案:

答案 0 :(得分:1)

我认为你只需要反序列化为集合而不是单个对象,例如

var searchResults = Newtonsoft.Json.JsonConvert.DeserializeObject<IEnumerable<SearchResult>>(t2, new Newtonsoft.Json.JsonSerializerSettings() { NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore })

searchResults将是您的SearchResult对象的IEnumerable。