Hi @Paul
My thoughts are:
- Declare classes and lists.
- Assign a value to the data.
- Generate JSON variables.
- Print it out
For demonstration purposes, I printed the JSON data as string data.
In order to keep the same format as JSON, I set an option.
var options = new JsonSerializerOptions { WriteIndented = true };
string jsonString = System.Text.Json.JsonSerializer.Serialize(outputdata, options);
Of course, you can also print it directly.
You can refer to the following code:
public class TASK_O
{
public List<TASK_O_ITEM> TASK_O_ITEM { get; set; }
}
public class Outputdata
{
public TASK_O TASK_O { get; set; }
public string ERROR_O { get; set; }
}
public class Example
{
public Outputdata Outputdata { get; set; }
}
public class TASK_O_ITEM
{
public string CODE { get; set; }
public string NAME { get; set; }
}
internal class Program
{
static void Main(string[] args)
{
var task_o_item = new List<TASK_O_ITEM> {
new TASK_O_ITEM
{
CODE = "123",
NAME = "ABC"
},
new TASK_O_ITEM
{
CODE = "456",
NAME = "DEF"
},
new TASK_O_ITEM
{
CODE = "789",
NAME = "GHI"
},
};
var task_O = new TASK_O
{
TASK_O_ITEM = task_o_item
};
var outputdata = new Outputdata
{
TASK_O = task_O,
ERROR_O = null
};
var example = new Example
{
Outputdata=outputdata
};
var options = new JsonSerializerOptions { WriteIndented = true };
string jsonString = JsonSerializer.Serialize(example, options);
Console.WriteLine(jsonString);
Console.Read();
}
}
Result:
Best Regards
Qi You
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.