1

Example: I have 3 classes

    public class A
    {
        public int id;
        public string data;
    }

    public class B
    {
        public int id;
        public List<C> data;
    }

    public class C
    {
        public int test;
    }

Usage:

    var a = new A
    {
        id = 21,
        data = "[{\"test\": 123}]"
    };

    var text = JsonSerializer.Serialize(a);

    var c = JsonSerializer.Deserialize<B>(text);

Class A i'm using to store data in DynamoDb, class B and C as a return type;

How to properly map json array inside json to class B I were trying to use default JsonSerializer and NewtonsoftJson, but both of them threw exceptions

dbc
  • 104,963
  • 20
  • 228
  • 340
Denied5
  • 91
  • 10
  • 1
    Add `[JsonConverter(typeof(EmbeddedLiteralConverter>))]` to `Data` where `EmbeddedLiteralConverter` comes from [this answer](https://stackoverflow.com/a/51850391/3744182) to [How to escape embedded JSON after unescape](https://stackoverflow.com/q/51845381/3744182). or [this answer](https://stackoverflow.com/a/39154630/3744182) to [How do I convert an escaped JSON string within a JSON object?](https://stackoverflow.com/q/39154043/3744182) if you only need deserialization. In fact I think this is a duplicate, agree? – dbc Dec 13 '21 at 17:41
  • Or, for System.Text.Json see [How do I deserialize a nested JSON object which is a string in System.Text.Json?](https://stackoverflow.com/q/69506159/3744182). It's unclear from your question which you want to use. – dbc Dec 13 '21 at 17:55
  • @dbc First of all, thank you a lot. You really help me. And yes, it's a duplication. Sorry for that! I have created a flag for duplication. – Denied5 Dec 14 '21 at 08:05
  • No problem, glad to help. But, which one answered your question? The Json.NET or System.Text.Json questions? – dbc Dec 14 '21 at 08:08
  • System.Text.Json – Denied5 Dec 14 '21 at 08:09

1 Answers1

0

should be somethinq like this

var c=new C(){ test=..};
var b=new B() { id=.., data= new List<C>{c}};

 var a = new A
    {
        id = b.Id,
        data = JsonSerializer.Serialize(b.data)
    };

or maybe this ( it is really hard to understand what do you want)

var b= new B
{
     it= a.Id,
     data=JsonSerializer.Deserialize<List<C>>(a.data)
}
Serge
  • 40,935
  • 4
  • 18
  • 45
  • Hi, we have found out that this question is a duplicate. Sorry for that. PS: If you are looking for an answer check the comments :) – Denied5 Dec 14 '21 at 08:11