如何从数据库创建嵌套的json?

时间:2019-04-15 08:29:20

标签: c# json

我创建了一个Web api,该数据库以json格式显示数据库中表中的数据。我想创建一个嵌套的api。

我该怎么做?

调用api时的json:

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

我想要这样的东西

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

我的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);
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您可以构建一个包装器类,该包装器类映射到所需的json结构,将其填充并返回包装器,而不是从控制器返回实体。您的包装器看起来像这样:

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; }
}

要能够轻松创建与给定json匹配的类结构,可以使用json2csharp之类的工具。

映射数据并返回它:

[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;
}

要拥有更简洁的操作方法,我建议编写一种扩展方法以将实体映射到其包装对象:

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 ...
    }
}

然后在这样的动作中调用它:

[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();
    }
}

答案 1 :(得分:0)

要获取您要求的结果json,必须使用以下类:

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; }
}

在控制器中,您必须执行以下操作:

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