【发布时间】: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