2

I have created a web api which displays data from a table in database in json format. I want to create a nested api.

How can I do this?

My json when I call the api:

{
   "ID":1,
   "plVersion":1,
   "mID":10,
   "sID":1025,
   "orID":null,
   "x":22.9935,
   "y":40.5885
}

I want something like this

[
   {
      "ID":1,
      "header":{
         "plVersion":1,
         "mID":10,
         "sID":1025
      },
      "mContainer":{
         "aID":{
            "orID":null
         },
         "Position":{
            "x":22.9935,
            "y":40.5885
         }
      }
   }
]

My testController.cs

public class testController : ApiController
{
    [Route("api/test")]
    public IEnumerable<test> Get()
    {
        using (Raw_DataEntities entities = new Raw_DataEntities())
        {
            return entities.tests.ToList();
        }
    }

    [Route("api/test/{id}")]
    public test Get(int id)
    {
        using (Raw_DataEntities entities = new Raw_DataEntities())
        {
            return entities.tests.FirstOrDefault(e => e.ID == id);
        }
    }
}
1
  • 1
    You must structure your resulting entity in the way you want your JSON to appear. So, build yourself classes that match your JSON, map your db entity to the class structure and return the mapped object structure instead of your db entity. Commented Apr 15, 2019 at 8:31

2 Answers 2

2

You can build a wrapper class which maps to your wanted json structure, fill it and return the wrapper instead of the entity from your controller. Your wrapper would look something like this:

public class Header
{
    public int plVersion { get; set; }
    public int mID { get; set; }
    public int sID { get; set; }
}

public class AID
{
    public object orID { get; set; }
}

public class Position
{
    public double x { get; set; }
    public double y { get; set; }
}

public class MContainer
{
    public AID aID { get; set; }
    public Position Position { get; set; }
}

public class RootObject
{
    public int ID { get; set; }
    public Header header { get; set; }
    public MContainer mContainer { get; set; }
}

To be able to easily create matching class structure to a given json you can use a tool like json2csharp.

Mapping the data and returning it:

[Route("api/test/{id}")]
public test Get(int id)
{
    using (Raw_DataEntities entities = new Raw_DataEntities())
    {
        var entity = entities.tests.FirstOrDefault(e => e.ID == id);
    }

    var root = new RootObject();
    // Filling the object
    root.ID = entity.Id;
    // etc. ...

    return root;
}

To have cleaner action methods, I would suggest to write an extension method for mapping the entity to its wrapper object:

public static class EntityExtensions
{
    public static RootObject ToJsonWrapper(this Entity entity)
    {
        if (entity == null)
            return null;

        var root = new RootObject();
        root.ID = entity.Id;
        // etc ...
    }
}

Then call it in the action like this:

[Route("api/test/{id}")]
public test Get(int id)
{
    using (Raw_DataEntities entities = new Raw_DataEntities())
    {
        return entities.tests.FirstOrDefault(e => e.ID == id).ToJsonWrapper();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

To obtain the resulting json you asked, you must use these classes:

public class Result
{
    public int ID { get; set; }
    public Header header { get; set; }
    public MContainer mContainer { get; set; }
}
public class Header
{
    public int plVersion { get; set; }
    public int mID { get; set; }
    public int sID { get; set; }
}
public class MContainer
{
    public AID aID { get; set; }
    public Position Position { get; set; }
}
public class AID
{
    public int? orID { get; set; }
}
public class Position
{
    public int x { get; set; }
    public int y { get; set; }
}

And in your controller you must do something like:

return entities.tests.Select(x => new Result {
    //Conversion
}).ToList();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.