Answer the question
In order to leave comments, you need to log in
How to create and populate array automatically and add it to json?
It is necessary to send an array of data via api json in C#.
Request example:
string json = $"{{\\"ret\":{{\"Words\":[\"массив данных\"]}}}}";
Answer the question
In order to leave comments, you need to log in
It is not specified whether you are writing in .Net Framework or .NET Core (.NET 5), but here is a solution for .NET 5:
1. Create a Ret class with a Words property:
class Ret
{
public Ret()
{
Words = new List<string>();
}
public List<string> Words { get; set; }
}
class RequestData
{
public RequestData()
{
Ret = new Ret();
}
[JsonPropertyName("ret")]
public Ret Ret { get; set; }
}
RequestData rd = new RequestData();
rd.Ret.Words.Add("Hello");
rd.Ret.Words.Add("World!");
var json = JsonSerializer.Serialize(rd);
You can create a class that has a property:
Public Property Words as List(Of String)
After creating an instance of the class and populating the Words, you can serialize the class to Json. To do this, you can use JavaScriptSerializer or newtonsoft.json
The output will be json
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question