0

We are deserializing Json files with Newtonsoft JSON. How do you use the JsonConvert.DeserializeObject<T>() to deserialize a JSON, which in turn contains a JSON?

{
    "character": "Someone",
    "json": "{\"test\":true}"
}

We created a type for the nested JSON and decorated it with JsonConverter(typeof(JsonConverter)) or JsonConverter(typeof(JsonConverter<PianoRhythmLessonData>)) but only got errors about missing constructor variants without parameter.

Serge
  • 40,935
  • 4
  • 18
  • 45
  • Welcome to stackoverflow! Have a look at this answer https://stackoverflow.com/a/17038878/3952573 which I believe holds the answer to what you're looking for. Also have a look at how you get the best results for your questions here: https://stackoverflow.com/help/asking – Pavlos Jul 05 '22 at 10:38
  • Unfortunately not. They deserialize just MyData, which does not contain another JSON like we do –  Jul 05 '22 at 10:48
  • My Bad, could you provide an example of the PianoRhythmLessonData object. – Pavlos Jul 05 '22 at 11:12
  • `public class PianoRhythmLessonData { public bool test; }` –  Jul 05 '22 at 12:47
  • If you only need to deserialize your embedded double-serialized JSON see [How do I convert an escaped JSON string within a JSON object?](https://stackoverflow.com/q/39154043/3744182). If you need to deserialize and re-serialize see [How to escape embedded JSON after unescape](https://stackoverflow.com/q/51845381/3744182). – dbc Jul 05 '22 at 14:11
  • Both have answers that introduce an `EmbeddedLiteralConverter` which you can apply to your `json` property like so `[JsonConverter(typeof(EmbeddedLiteralConverter))] public PianoRhythmLessonData Json { get; set; }` – dbc Jul 05 '22 at 14:14

0 Answers0