I've been trying to nest a set of objects inside my json array and I can't seem to get it to work.
{
"events":[
{
"event_name":"purchase",
"event_params":{
"product_id":"AGH210070",
"unit_price":990.9,
"currency":"USD"
}
},
{
"event_name":"purchase",
"event_params":{
"product_id":"AGH210020",
"unit_price":890,
"currency":"EUR"
}
}
]
}
Here's the code I'm using to do it:
using (SqlConnection con = new SqlConnection(connString))
{
string transactionShipQuery = String.Format(@"randomsqlquery");
SqlCommand shipsCmd = new SqlCommand(transactionShipQuery, con);
con.Open();
SqlDataReader ships = shipsCmd.ExecuteReader();
while (ships.Read())
{
event_name = ships.GetString(0);
model.Events.Add(new EventModel
{
Event_name = event_name
});
}
}
using (SqlConnection con = new SqlConnection(connString))
{
string transactionShipQuery = String.Format(@"sqlquery");
SqlCommand shipsCmd = new SqlCommand(transactionShipQuery, con);
con.Open();
SqlDataReader ships = shipsCmd.ExecuteReader();
while (ships.Read())
{
product_id = ships.GetString(0);
unit_price = ships.GetDecimal(1);
currency = ships.GetString(2);
model.Events.Event_params.Product_id = product_id;
model.Events.Event_params.Unit_price = product_id;
model.Events.Event_params.Currency = product_id;
}
}
var myJson = JsonConvert.SerializeObject(model, Formatting.Indented);
Console.WriteLine(myJson);
And here is my class structure:
[Serializable]
public class model
{
[JsonProperty("events")]
public Collection<EventModel> Events = new Collection<EventModel>();
}
public class EventModel
{
[JsonProperty("event_name")]
public string Event_name { get; set; }
[JsonProperty("event_params")]
public ProductModel Event_params { get; set; } = new ProductModel();
}
public class ProductModel
{
[JsonProperty("product_id")]
public string Product_id { get; set; }
[JsonProperty("unit_price")]
public decimal Unit_price { get; set; }
[JsonProperty("currency")]
public string Currency { get; set; }
}
I can't seem to find a way to call event_params inside of the events collection without getting an error saying the collection does not contain a definition for it.
model.Events.Event_params
you are trying to access Event_params on a collection instead of the actual object you want.
you could for example change it to:
int i = 0
while (ships.Read())
{
product_id = ships.GetString(0);
unit_price = ships.GetDecimal(1);
currency = ships.GetString(2);
model.Events[i].Event_params.Product_id = product_id;
model.Events[i].Event_params.Unit_price = product_id;
model.Events[i].Event_params.Currency = product_id;
i++;
}
In your second using statement you are using Events collection as-if it is an object. You have to selecte the correct Event from the collection and then assing properties to it. See below code:
class Program
{
static void Main(string[] args)
{
var _model = new model();
//First using statement creates an event
_model.Events.Add(new EventModel
{
Event_name = "event_name"
});
//First using statement search for appropriate Event and assign values, I am sure you have Event name while running second SQl query
var _event = _model.Events.FirstOrDefault(x => x.Event_name == "event_name");
if (_event != null)
{
_event.Event_params.Product_id = "product_id";
_event.Event_params.Unit_price = (decimal)1.30;
_event.Event_params.Currency = "USD";
}
var myJson = JsonConvert.SerializeObject(_event, Formatting.Indented);
Console.WriteLine(myJson);
Console.ReadLine();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.