I have this code
var httpWebRequestAuthentication = (HttpWebRequest)WebRequest.Create("http://api");
httpWebRequestAuthentication.ContentType = "application/json";
httpWebRequestAuthentication.Accept = "en";
httpWebRequestAuthentication.Headers.Add("Accept-Language", "en");
httpWebRequestAuthentication.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequestAuthentication.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
agent_name = "my name",
agent_password = "myPassword",
countryCode = "US",
requestType = "post",
sales_representatives = new[] { // How do I create here a Foreach loop that will iterate a C# collection and create the JSON array?
new {
product = "agent1",
primary_sales_representative= 1234,
secondary_sales_representative= 2345
},
new {
product = "agent2",
primary_sales_representative = 1111,
secondary_sales_representative= 2222
}
}
});
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponseAuthentication = (HttpWebResponse)httpWebRequestAuthentication.GetResponse();
using (var streamReaderAuthentication = new StreamReader(httpResponseAuthentication.GetResponseStream()))
{
var resultAuthentication = streamReaderAuthentication.ReadToEnd();
}
I want to change this code so my sales_represetatives JSON collection will be created from my c# list of sales representatives.
I couldn't find a way to insert a foreach loop in this code to create the JSON array?