I have the following classes and I need to fill myDATA.Add(......)
public class AAClass
{
public long ts { get; set; }
public DateTime value { get; set; }
}
public class BBClass
{
public long ts { get; set; }
public string value { get; set; }
}
public class MyRootClass
{
public List<AAClass> AA { get; set; }
public List<BBClass> BB { get; set; }
}
void Main()
{
var myData = new List<MyRootClass>();
myData.Add(new MyRootClass() { AA = new AAClass { ts = 1636862399574, value = "2021-11-14 00:57:25.04998" }});
myData.Add(new MyRootClass() { AA = new AAClass { ts = 1636862398995, value = "2021-11-14 00:57:24.049979" }});
myData.Add(new MyRootClass() { BB = new BBClass { ts = 1636862399574, value = "16183.8" }});
myData.Add(new MyRootClass() { BB = new BBClass { ts = 1636862398995, value = "16250.8" }});
}
My code in void Main () returns an error because I am not sure how to add the data.
I have the error in this line:
myData.Add(new MyRootClass() { ........... });
My purpose is to be able to fill the list to finally obtain this:
{
"AA": [
{
"ts": 1636862399574,
"value": "2021-11-14 00:57:25.049983"
},
{
"ts": 1636862398995,
"value": "2021-11-14 00:57:24.049979"
}
],
"BB": [
{
"ts": 1636862399574,
"value": "16183.8"
},
{
"ts": 1636862398995,
"value": "16250.8"
}
]
}
CodePudding user response:
It's not that I'm not impressed by the amount of code you can write for no reason, but if all you want is to generate the JSON you pasted in your question, then this is more than enough:
JsonSerializer.Serialize(new
{
AA = new[]
{
new { ts = 1636862399574, value = "2021-11-14 00:57:25.049983" },
new { ts = 1636862398995, value = "2021-11-14 00:57:24.049979" },
},
BB = new[]
{
new { ts = 1636862399574, value = "16183.8" },
new { ts = 1636862398995, value = "16250.8" }
}
});
CodePudding user response:
You have a few issue, you are initialing it wrong, also you cant use the word value
as a variable name as it is a reversed keyword in C#
using System;
using System.Collections.Generic;
public class Program
{
public class AAClass
{
public long ts { get; set; }
public DateTime _value { get; set; }
}
public class BBClass
{
public long ts { get; set; }
public string _value { get; set; }
}
public class MyRootClass
{
public List<AAClass> AA { get; set; }
public List<BBClass> BB { get; set; }
}
public static void Main()
{
var myData = new List<MyRootClass>();
myData.Add(new MyRootClass());
myData[0].AA = new List<AAClass>();
myData[0].AA.Add(new AAClass() { ts = 1636862399574, _value = DateTime.Parse("2021-11-14 00:57:25.04998") });
myData.Add(new MyRootClass());
myData[1].AA = new List<AAClass>();
myData[1].AA.Add(new AAClass() { ts = 1636862398995, _value = DateTime.Parse("2021-11-14 00:57:24.049979") });
myData.Add(new MyRootClass());
myData[2].BB = new List<BBClass>();
myData[2].BB.Add(new BBClass() { ts = 1636862399574, _value = "16183.8" });
myData.Add(new MyRootClass());
myData[3].BB = new List<BBClass>();
myData[3].BB.Add(new BBClass() { ts = 1636862398995, _value = "16250.8" });
}
}
However as you want a certian result, I believe you actually want code that looks like this
public static void Main()
{
var myData = new MyRootClass();
myData.AA = new List<AAClass>();
myData.BB = new List<BBClass>();
myData.AA.Add(new AAClass() { ts = 1636862399574, _value = DateTime.Parse("2021-11-14 00:57:25.04998") });
myData.AA.Add(new AAClass() { ts = 1636862398995, _value = DateTime.Parse("2021-11-14 00:57:24.049979") });
myData.BB.Add(new BBClass() { ts = 1636862399574, _value = "16183.8" });
myData.BB.Add(new BBClass() { ts = 1636862398995, _value = "16250.8" });
}