【问题标题】:How to struct Json format to return two arrays in Web API MVC如何在 Web API MVC 中构造 Json 格式以返回两个数组
【发布时间】:2018-07-15 00:37:51
【问题描述】:

我想回到 JSON 格式两个单独的数组。现在,我返回一个 List 并在那里填写所有数据。

如何创建一个包含两个数组的对象,并在一个数组中填充 Station、Ime 和 Cell,并在另一个数组中填充 TA、RH、WS、SR、APRES。

我有一堂课有 3 个对象。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebAPI2.Models

{

public class Stations
{
    public string Station { get; set; }

    public string Ime { get; set; }

    public string Cell { get; set; }



  }

public class otherData
{
    public string DATS { get; set; }

    public string TA { get; set; }

    public string RH { get; set; }

    public string WS { get; set; }

    public string SR { get; set; }

    public string APRES { get; set; }
}

public class getAllValues
{
    public List<Stations> Stations { get; set; }

    public List<otherData> Data { get; set; }
    }
}

在 ValuesController 中,我创建了两个带有两个单独列表的 foreach,但我不知道如何返回它们。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MySql.Data.MySqlClient;
using System.Data;
using WebAPI2.Models;

namespace WebAPI2.Controllers
{

[RoutePrefix("API/Stations")]

public class ValuesController : ApiController
{

    [Route("")]
    // GET api/values

    public IEnumerable<getAllValues> Get()
    {
        MySqlConnection conn = WebApiConfig.conn();

        try
        {
            conn.Open();
        }
        catch (MySql.Data.MySqlClient.MySqlException ex)
        {
            string err = "Error: " + ex;
        }

        MySqlCommand executeStations = new MySqlCommand("select Stationsall.station,stationsall.Ime,Cell from stationsall join stations_cells using (Station);", conn);

        MySqlCommand executeData = new MySqlCommand("CALL Get_mod_cell_values_meteogram ('2018-01-27 00:00:00',42020,5);", conn);

        MySqlDataAdapter da = new MySqlDataAdapter(executeStations);

        MySqlDataAdapter da1 = new MySqlDataAdapter(executeData);

        DataTable dt = new DataTable();

        DataTable dt1 = new DataTable();

        da.Fill(dt);

        da1.Fill(dt1);

        List<Stations> Stations = new List<Stations>();

        foreach (DataRow dr in dt.Rows)
        {
            Stations getAllStations = new Stations
            {
                Station = dr["station"].ToString(),
                Ime = dr["Ime"].ToString(),
                Cell = dr["Cell"].ToString()
            };

            Stations.Add(getAllStations);              

            //returnedData(dr["station"].ToString(), dr["Ime"].ToString(), dr["Cell"].ToString());
        }

        List<otherData> otherData = new List<otherData>();

        foreach (DataRow dr1 in dt1.Rows)
        {
            otherData getAllData = new otherData
            {
                DATS = dr1["DATS"].ToString(),
                TA = dr1["TA"].ToString(),
                RH = dr1["RH"].ToString(),
                WS = dr1["WS"].ToString(),
                SR = dr1["SR"].ToString(),
                APRES = dr1["APRES"].ToString()
            };

            otherData.Add(getAllData);
        }

        return /*how to return otherData and Stations ?*/;


        }      
    }
}

【问题讨论】:

  • 你可能有 2 个额外的类,你的第一个数据在一个类中,第二个数据在另一个类中,并在你的 Station 类中引用这两个类
  • 你能举例解释一下吗? :) 我真的不知道到底是怎么回事 :)

标签: c# arrays json object asp.net-web-api


【解决方案1】:

您可以为所有数据创建单独的视图模型。 你需要3个类: 第一个)

public class StationViewModel
{

    public string Station { get; set; }

    public string Ime { get; set; }

    public string Cell { get; set; }
}

第二次

public class DataViewModel
{
    public string DATS { get; set; }

    public string TA { get; set; }

    public string RH { get; set; }

    public string WS { get; set; }

    public string SR { get; set; }

    public string APRES { get; set; }
}

第三

public class TableViewModel
{
     public List<StationViewModel> Stations { get; set; }

     public List<DataViewModel> Data { get; set; }
}

然后将 Yours 对象映射到新的视图模型。

编辑

试试这个:

var getAllValuesList = new List<getAllValues>()
{
    new getAllValues()
    {
        Stations = Stations,
        Data  = otherData 
    }
};
return getAllValuesList;

【讨论】:

  • 我用你的代码更新了我的问题,但我想要更多一点来构造两个数组:)
  • @ВалерияБлаговест 如果您想拥有单独的表,请不要从其他数据继承站点。当您继承时,您只需将其他数据的属性添加到站
  • 好的,但是如何返回这两个列表?
  • 你想达到什么目的?要分隔具有彼此关系的列表?
  • db 中的数据结构是什么样的?这些数据的主键是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多