Не меняется название поля в JSON файле
Я создал кортеж и именовал поля. После заполнения кортежа сделал сериализацию и записал в файл, но когда я просмотрел этот файл там вместо именнованых полей Item.
Программный код:
public class AirPollution
{
public string post;
public class Indication
{
public string aqi;
public string CO;
public string PM2and5;
public string PM10;
public DateTime date = DateTime.Now;
}
}
class Program
{
static void Main(string[] args)
{
string[] pos = new string[] { "Пост Кирова", "Пост Маршала Жукова", "Пост Ленина", "Пост Первомайская", "Пост Крылова" };
AirPollution.Indication [] indicatepoll = new AirPollution.Indication[pos.Length];
AirPollution[]pol = new AirPollution[pos.Length];
(string aqi, string CO, string PM2and5, string PM10)[][] tuples = new (string aqi, string CO, string PM2and5, string PM10)[pos.Length][];
(string post, (string, string, string, string)[] indication)[] tuple = new (string post, (string, string, string, string)[] indication)[pos.Length];
Random rand = new Random();
for (int i = 0; i < pos.Length; i++)
{
pol[i] = new AirPollution();
pol[i].post = pos[i];
poll[i] = new AirPollution.Indication();
poll[i].aqi = Convert.ToString(rand.Next(0, 500));
poll[i].CO = Convert.ToString(rand.Next(0, 20) + "%");
poll[i].PM2and5 = Convert.ToString(rand.Next(0, 20) + "%");
poll[i].PM10 = Convert.ToString(rand.Next(0, 20) + "%");
for (int j = 0; j < 1; j++)
{
tuples[i] = new (string aqi, string CO, string PM2and5, string PM10)[1];
tuples[i][j] = (poll[i].aqi, poll[i].CO, poll[i].PM2and5, poll[i].PM10);
tuple[i] = (pol[i].post, indication:tuples[i]);
}
}
string js = JsonConvert.SerializeObject(tuple);
StreamWriter writer = new StreamWriter("C:\\Users\\jjkjk\\Desktop\\DataSimulation.json");
writer.Write(js);
writer.Close();
}
}
Через класс я пытался сделать так:
public class AirPollution
{
public string post;
public string aqi;
public string CO;
public string PM2and5;
public string PM10;
public DateTime date = DateTime.Now;
}
class Program
{
static void Main(string[] args)
{
string[] pos = new string[] { "Пост Кирова", "Пост Маршала Жукова", "Пост Ленина", "Пост Первомайская", "Пост Крылова" };
AirPollution[] pol = new AirPollution[pos.Length];
Random rand = new Random();
for (int i = 0; i < pos.Length; i++)
{
pol[i] = new AirPollution();
pol[i].post = pos[i];
pol[i].aqi = Convert.ToString(rand.Next(0, 500));
pol[i].CO = Convert.ToString(rand.Next(0, 20) + "%");
pol[i].PM2and5 = Convert.ToString(rand.Next(0, 20) + "%");
pol[i].PM10 = Convert.ToString(rand.Next(0, 20) + "%");
}
string js = JsonConvert.SerializeObject(pol);
StreamWriter writer = new StreamWriter("C:\\Users\\jjkjk\\Desktop\\DataSimulation.json");
writer.Write(js);
writer.Close();
}
}
В этом случае структура в json выходит такая:
[{"post":"Пост Кирова","aqi":"161","CO":"3%","PM2and5":"7%","PM10":"2%","date":"2020-05-18T17:21:57.6419238+07:00"}]
А нужна такая:
[{"post":"Пост Кирова","Indication":[{"aqi":"418","CO":"19%","PM2and5":"3%","PM10":"6%", "date":"2020-05-18T17:21:57.6419238+07:00"}]}