1

Im creating a simple API to get some data from a MongoDB, but the original code that is saving the data in that DB is writen in Java, so the saved document is like this:

{
    "_id" : ObjectId("2cd5e357764b650001c6f53c"),
    "rechargeValue" : 5000,
    "eventsReceived" : [ 
        {
            "hostId" : "000001234567",
            "localDateTime" : ISODate("2019-05-10T20:46:42.000Z"),
            "serverDateTime" : ISODate("2019-05-10T20:45:24.000Z"),
            "_class" : "br.com.domain.DoSaleRepresentation"
        }, 
        {
            "paymentId" : "PAY-a443a793-74f7-43a5-85ad-4e02f0c8fdab",
            "_class" : "br.com.domain.PaymentRepresentation"
        }, 
        {
            "localDateTime" : ISODate("2019-05-10T20:46:46.000Z"),
            "serverDateTime" : ISODate("2019-05-10T20:45:28.000Z"),
            "_class" : "br.com.domain.SaleRepresentation"
        }
    ],
    "_class" : "br.com.model.Data"
}

It's easier to convert it in Java, since the Mongo driver uses the "_class" property to map the data into the correct object, but I'm having problems to map it to a C# class.

Since "eventsReceived" is a "object" List, the 3 objects in there are different, but with the same type "object", and I want to know a way to map each of those objects to an specific class in my C# so I can get the values inside them, like:

var paymentID = Data.EventsReceived.PaymentRepresentation.PaymentId;
Jose Will
  • 31
  • 2

0 Answers0