0

I get external json data and want to deserialize to my collection:

        var responseString = await response.Content.ReadAsStringAsync();

        var result = JsonParse.ParseArrayCamelCase<ReadOnlyCollection<SureMdmGroup>>(responseString, "Groups");
        return result;

where ParseArrayCamelCase is:

    public static T ParseArrayCamelCase<T>(string txt, string arrayName)
    {
        JObject json = JObject.Parse(txt);
        var items = json[arrayName].ToString();
        JsonSerializerSettings serSettings = new JsonSerializerSettings
        {
            ContractResolver = new CamelCasePropertyNamesContractResolver(),
            Formatting = Formatting.Indented,
            StringEscapeHandling = StringEscapeHandling.EscapeHtml
        };
        return JsonConvert.DeserializeObject<T>(items, serSettings);
    }

it works, but if some string like

E&amp;B Transport, LLC.

it should be converted to

E&B Transport, LLC.

What should I set for JsonSerializerSettings to decode strings?

Oleg Sh
  • 8,496
  • 17
  • 89
  • 159
  • 1
    [decode-html-encoded-characters-during-json-deserialization](https://stackoverflow.com/questions/35973032/decode-html-encoded-characters-during-json-deserialization) – Ryan Wilson May 14 '21 at 14:09
  • @RyanWilson no way to set converter and settings at once (I need to set `CamelCasePropertyNamesContractResolver` also) – Oleg Sh May 14 '21 at 14:10
  • So you'd use the suggested class(es) from the linked answer to decode the HTML in the Json string before deserializing into your concrete objects, you'd do this in your custom deserialization before Parsing. There is no built in option for this. – Ryan Wilson May 14 '21 at 14:12
  • @RyanWilson I don't know how to do it before deserialization because I have only raw string before deserialization – Oleg Sh May 14 '21 at 14:24
  • Run the raw string through the method exposed in one of the classes listed in the link above, then deserialize. I'm not sure how much more clear I can be on this. – Ryan Wilson May 14 '21 at 14:46
  • @RyanWilson oh, you mean it... ok, don't like such solutions :) Thanks in anyway! – Oleg Sh May 14 '21 at 18:20

0 Answers0