1

I have an issue to deserialize a JSON to an object, i have been searching around and found the following 2 question

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

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

the Json response

[
  {
    "$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"
  }
]

My Class look like

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

when i try to follow the first question (which it is to remove the [] from the beginning and the end of the json response) it will work if the Json only contain information about one company like

 [
  {
    "$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"
  }
]

when there is more information like the above Json i get the Error 'Additional text encountered after finished reading JSON content: and i am trying to deserilazing the json using the code below

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

any help would be appreciated, because i am stuck

Reza Del
  • 763
  • 10
  • 30

1 Answers1

1

I think you just need to deserialize to a collection rather than a single object e.g.

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

searchResults will be an IEnumerable of your SearchResult objects.

AGB
  • 2,378
  • 21
  • 37
  • Thank you very much that did work, because i am new i will ask you a dumb question, why does this happens? i never had this issue before and i was using the same method for other API calls – Reza Del Aug 03 '17 at 09:49
  • You were trying to convert some JSON that represented an array into an object which represented a single item, which can't possibly work. – AGB Aug 03 '17 at 09:52
  • Probably because you weren't specifying the type to deserialize to before? I see you were using a dynamic before which suggests that may have been the case. – AGB Aug 03 '17 at 09:55