我正在开发一个web api项目,我希望返回非特定的json类型。我正在返回两个不同的数组,但我希望它与我下面的类型一样。
Snippet from my controller
public IHttpActionResult GetNOX(){
-------
List<ListCreator> lisss = new List<ListCreator>();
lisss.Add(new ListCreator(ısavg, ossbavg, kadavg, benzinavg, dolumavg, harfiyatavg, madenavg));
return Ok(lisss);
}
------------------
public class ListCreator
{
public List<double> avg = new List<double>();//ArayLists which contains the industrial sectors and their gas emissions
public List<string> names = new List<string>();
public ListCreator(double ısınma, double osb, double kad, double benzin, double dolum, double harfiyat, double maden)//Consructor
{
avg.Add(ısınma);//Adds sector names to arraylist
avg.Add(osb);
avg.Add(kad);
avg.Add(benzin);
avg.Add(dolum);
avg.Add(harfiyat);
avg.Add(maden);
names.Add("Isinma");
names.Add("OSB");
names.Add("KAD");
names.Add("Benzin");
names.Add("Dolum");
names.Add("Harfiyat");
names.Add("Maden");
}
我的输出是
[{"avg":[384925464640.5,1215183866.964,255.0,85284076.039996013],"names": ["Isinma","OSB","Benzin","Harfiyat"]}]
我希望它的形式为
data: [
['Isınma Kaynaklı', 56.0],
['OSB', 21.0],
['KAD', 2.3],
['Benzin İstasyonu', 25.0],
['Dolum Tesisleri', 18.6],
['Harfiyat Alanları', 11.8],
['Maden Taş Ocakları', 12.7]
]
答案 0 :(得分:0)
似乎您希望将name
与avg
相关联。
我在这里要做的是创建一个包含这两个字段的新类,并封装你想要建模的内容。
像
这样的东西public class Model
{
public string Name { get; set; }
public double Average { get; set; }
}
然后在您的控制器中,您可以返回一个列表
public IHttpActionResult Get()
{
List<Model> models = new List<Model>
{
new Model
{
Name = "Name",
Average = "10.0"
}
};
return models;
}
客户端上的JSON看起来像(取决于您使用的json格式化程序)
[
{ name: 'name', average: '10.0' }
]