返回多个 JSON 作为 HttpResponseMessage

发布于 2025-01-17 13:53:22 字数 983 浏览 0 评论 0原文

我有一个 HttpResponseMessage 方法,它返回基于数据库数据的 JSON:

public HttpResponseMessage RespMsg(JObject jsonData)
{
   HttpResponseMessage response = new HttpResponseMessage();
   dynamic json = jsonData;
   int recId = jsonData.Id;
   var respStructure = myTable.Where(r => r.Id==recId).Select(t => new 
      {
         t.Id,
         t.Name
      }
   var responseJson = JsonConvert.SerializeObject(respStructure);
   response.Content = new StringContent(responseJson, null, "application/json");

   return response;
}

我得到的响应类似于 {"Id":3,"Name":Third}。 T1 中的行在表 T2 中有多行 有

var t2Resp = T2.Where(c => c.T1Id == recId);

foreach (var t in t2Resp) {
 //call a method that return an object with computed data
}

没有办法将 foreach 中的数据作为单独的 JSON 添加,例如 {"Id":3,"名称":"第三个"} {foreach 中的第一个数据} {foreach 中的第二个数据}?第一个来自 T1 查询,下一个取决于 t2Resp 长度

I have a HttpResponseMessage method that returns a JSON based on DB data:

public HttpResponseMessage RespMsg(JObject jsonData)
{
   HttpResponseMessage response = new HttpResponseMessage();
   dynamic json = jsonData;
   int recId = jsonData.Id;
   var respStructure = myTable.Where(r => r.Id==recId).Select(t => new 
      {
         t.Id,
         t.Name
      }
   var responseJson = JsonConvert.SerializeObject(respStructure);
   response.Content = new StringContent(responseJson, null, "application/json");

   return response;
}

The response I get is something like {"Id":3,"Name":Third}.
The row in T1, has multiple rows in table T2

var t2Resp = T2.Where(c => c.T1Id == recId);

foreach (var t in t2Resp) {
 //call a method that return an object with computed data
}

Is there a way to add the data from foreach as separate JSON like {"Id":3,"Name":"Third"} {first data from foreach} {second data from foreach}? The first one is from T1 query and the next ones depending on t2Resp length

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝天 2025-01-24 13:53:22

提示:- 首先,您必须创建一个与您的响应匹配的 DTO 对象。由于 T1 和 T2 具有一对多关系,因此创建具有以下结构的 DTO 类。

 public  class DtoData
{
    public string id { get; set; }
    public string Name { get; set; }
    public List<DaatFromT2> T2Data { get; set; }
}

public class DaatFromT2
{
    public string prop1 { get; set; }
    public int prop2 { get; set; }
    public DateOnly prop3 { get; set; }
}

然后,您必须使用此类来填充 T1 和 T2 中的数据,最后将其灭菌为 JSON。如下图所示。

    var resposeStructure = new List<DtoData>();

    var t1data = myTable.Where(r => r.Id==recId).Select(t => new
    {
        t.Id,
        t.Name
    };
   var t2Resp = T2.Where(c => c.T2Id == recId);
   foreach (var t in t1data)
    {
        var data = new DtoData
        {
            id = t.Id,
            Name = t.Name,
            T2Data = t2Resp.Where(c => t.id == t2Resp.someid)
            .Select(t => new
            {
                //call a method that return an object with computed data
                //and map to corresponding properties
            });
        }
     resposeStructure.Append(data);
   }
  var responseJson = JsonConvert.SerializeObject(respStructure);

这些代码片段可能会给您一些想法并能够解决这个问题。
快乐编码:)

Hint:- First You have to create a DTO object that matches with your response. Since T1 and T2 have one-to-many relationship create a DTO class with below structure.

 public  class DtoData
{
    public string id { get; set; }
    public string Name { get; set; }
    public List<DaatFromT2> T2Data { get; set; }
}

public class DaatFromT2
{
    public string prop1 { get; set; }
    public int prop2 { get; set; }
    public DateOnly prop3 { get; set; }
}

Then you have to use this class to populate data from T1 and T2 and finally sterilize to JSON. Something as shown below .

    var resposeStructure = new List<DtoData>();

    var t1data = myTable.Where(r => r.Id==recId).Select(t => new
    {
        t.Id,
        t.Name
    };
   var t2Resp = T2.Where(c => c.T2Id == recId);
   foreach (var t in t1data)
    {
        var data = new DtoData
        {
            id = t.Id,
            Name = t.Name,
            T2Data = t2Resp.Where(c => t.id == t2Resp.someid)
            .Select(t => new
            {
                //call a method that return an object with computed data
                //and map to corresponding properties
            });
        }
     resposeStructure.Append(data);
   }
  var responseJson = JsonConvert.SerializeObject(respStructure);

May be theses code snippets give you some idea and able to sort this issue.
Happy coding :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文