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?
asked May 10, 2016 at 12:46
-
I would say this is a crude way of doing serialization. If you create a class with the desired output, you can simply use Newtonsoft Json to serialize the object. Do you need any samples to do it this way?Krishna Chaithanya Muthyala– Krishna Chaithanya Muthyala2016年05月10日 13:08:48 +00:00Commented May 10, 2016 at 13:08
-
Thank you @KrishnaChaithanyaMuthyala. the c# class I have is not identical to the JSON i need to POST. I also use this C# class to POST to other apis (different ones). is this information change your suggestion to use Newtonsoft?Shlo– Shlo2016年05月10日 13:20:31 +00:00Commented May 10, 2016 at 13:20
-
Either way, I think using Newtonsoft library is better.Krishna Chaithanya Muthyala– Krishna Chaithanya Muthyala2016年05月10日 13:22:10 +00:00Commented May 10, 2016 at 13:22
1 Answer 1
Just replace that code:
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
}
}
With the following one:
sales_representatives = yourCollection.Select(repr=>new {
product = repr.ProductField,
primary_sales_representative = repr.PrimaryField,
secondary_sales_representative = repr.SecondaryField
}
This assumes that your c# list of sales representatives is stored in a collection named "yourCollection", and that each object in that collection has ProductField, PrimaryField and SecondaryField properties. Change to your liking.
answered May 10, 2016 at 12:52
Sign up to request clarification or add additional context in comments.
1 Comment
Shlo
Thank you @Arnaud.
default