我想回到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 ?*/;
}
}
}
答案 0 :(得分:0)
您可以为所有数据制作separete视图模型。 你需要3个clases: 1)
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;