I have problem with architectural problem for my C# application. I need to have the following JSON:
{data:{lat:22,lng:33}, error:{code:0,description:""}}
I’m trying to implement this JSON structure with these classes:
public class Response
{
public object Data { get; set; }
public object Error { get; set; }
public Response()
{
Error = new ErrorsManager();
}
}
public class Coordinates : Response
{
public double Lat { get; set; }
public double Lng { get; set; }
}
I can set values for Error without any problems. But how to set values for Data I can’t understand. Can anybody suggest proper solution for this situation? I know that this problem can be solved with extends in Java. Can I use something similar in C#?
Update: I’d like to have common root objects in JSON – data and error. But data object can have different context like: coordinates, user data, etc.
You would set Data to an instance of coordinates. That is something like this:
You might think about creating strongly typed versions of Response meaning something like this:
If you have different response types, you might want to have a base class defining only Error and the subclasses adding a new property. I assume you’re using something like JSON.NET to serialize / deserialize.
You could be generic:
Usage: