I am tring to create nested json string here is my json string below,
[
{
'CompanyID':'1',
'Name':'Company1',
'DepartmentName':'Executive General and Administration',
'ModifiedDate':'2005-06-01 00:00:00.000',
},
{
'CompanyID':'2',
'Name':'Company2',
'DepartmentName':'Executive General and Administration',
'ModifiedDate':'2005-06-01 00:00:00.000',
},
{
'CompanyID':'3',
'Name':'Company3',
'DepartmentName':'Executive General and Administration',
'ModifiedDate':'2005-06-01 00:00:00.000',
}
]
and i want result like this
[
{
'CompanyID':'1',
'Name':'Company1',
department":{
'DepartmentName': 'Dpt1'
}
}
]
I want company wise department for this json string... I used Jarray but failed to do so...
my code:
string jsonCompany = "[{'CompanyID' : '1','Name' : 'Company1','DepartmentName' : 'D1','ModifiedDate' : '2005-06-01 00:00:00.000',},{'CompanyID' : '2','Name' : 'Company2','DepartmentName' : 'D2','ModifiedDate' : '2005-06-01 00:00:00.000',},{'CompanyID' : '1','Name' : 'Company3','DepartmentName' : 'D3','ModifiedDate' : '2005-06-01 00:00:00.000',}]";
string jsonDept = "[{'DepartmentID' : '91','Name' : 'Executive84','GroupName' : 'Executive General and Administration','ModifiedDate' : '2005-06-01 00:00:00.000',},{'DepartmentID' : '92','Name' : 'Executive85','GroupName' : 'Executive General and Administration','ModifiedDate' : '2005-06-01 00:00:00.000',},{'DepartmentID' : '93','Name' : 'Executive86','GroupName' : 'Executive General and Administration','ModifiedDate' : '2005-06-01 00:00:00.000',}]";
object dynJsonCmp = JsonConvert.DeserializeObject(jsonCompany);
dynamic dynJsonDept = JsonConvert.DeserializeObject(jsonDept);
One of the cleanest ways of such manipulation is with using classes. Based on the json you initially provided, and your initial question, you can do the following.:
Then you can simply deserialize into a list, convert it with the help of
Linq
and serialize itSince your code seems to show a different json, you may have to modify the Linq part according to what you are actually trying to do. You can look into Linq to check what exactly you are trying to do.