I have a List of string that contains the below items:
- 001-HA-Manager
- 001-HA-Supervisor
- 001-HA-Validation
- 001-HA-DocumentReviewer
- 001-HA-ManagerReviewer
- 002-HA-Manager
- 002-HA-Supervisor
- 002-HA-Validation
I need to those item on the below JSON format:
{
"Project": [
{
"ProjectCode": "001",
"Groups": ["Manager", "Supervisor", "Validation", "DocumentReviewer","ManagerReviewer"]
},
{
"ProjectCode": "002",
"Groups": ["Manager", "Supervisor", "Validation"]
}
]
}de here
3
Answers
you can try this code
Using a traditional approach without any one liners. I have created corresponding
Model
classes to hold your data and the tokenize it as required to fill out the respective fields into theModel
class:Fiddle: https://dotnetfiddle.net/XH28FX
Output:
{"Project":[{"ProjectCode":"001","Groups":["Manager","Supervisor","Validation","DocumentReviewer","ManagerReviewer"]},{"ProjectCode":"002","Groups":["Manager","Supervisor","Validation"]}]}
After validating from Jsonlint:
First create class
then ,define a constructor to receive the data and then separate the project and the group And then group the project and create Jason
I use Newtonsoft.Json and Linq
1.create class
2.Add data
3.group by with linq and create json