Can someone help me with this code snippet?
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 can I dynamically create a JSON array from a C# collection using a foreach loop?
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 need to modify the code so that my sales_represetatives
JSON collection is generated from a C# list of sales representatives.
Is there a way for me to include a foreach
loop in this code to achieve this?