Reputation: 31
I have to create a JSON string, which should look like this at the end:
{
"PersonId": "abc",
"Firstname": "Max",
"Lastname": "Muster",
"AddressLine1": "Hauptstrasse 3",
"ZipCode": "8000",
"City": "Zürich",
"ClubRelatedPersonDetails":
{
"IsGliderInstructor": true,
"IsGliderTrainee": false,
"IsTowPilot": true,
}
}
I already have two separate JObjects
:
pers
which contains all the name and address info (PersonId
through City
in the example above)clubPers
which contains the club-related info (IsGliderInstructor
through IsTowPilot
)So I have to add clubPers
as a JObject
to pers
, I think? How can I do this in VB.NET?
Upvotes: 2
Views: 1374
Reputation: 70327
If Option Strict and Option Explicit are off:
Dim pers = JObject.Parse(json1)
Dim clubPers = JObject.Parse(json2)
pers.ClubRelatedPersonDetails = clubPers
Console.WriteLine(pers.ToString())
If they are on, then use Brian Rogers' answer.
Bonus: in C# we write,
dynamic pers = JObject.Parse(json1);
dynamic clubPers = JObject.Parse(json2);
pers.ClubRelatedPersonDetails = clubPers;
Upvotes: 0
Reputation: 129787
Just use the Add
method on the first JObject
, providing the desired property name and the other JObject
as parameters.
For example:
Dim json1 As String = _
"{" + _
" ""PersonId"": ""abc""," + _
" ""Firstname"": ""Max""," + _
" ""Lastname"": ""Muster""," + _
" ""AddressLine1"": ""Hauptstrasse 3""," + _
" ""ZipCode"": ""8000""," + _
" ""City"": ""Zürich""" + _
"}"
Dim json2 As String = _
"{" + _
" ""IsGliderInstructor"": true," + _
" ""IsGliderTrainee"": false," + _
" ""IsTowPilot"": true" + _
"}"
Dim pers As JObject = JObject.Parse(json1)
Dim clubPers As JObject = JObject.Parse(json2)
pers.Add("ClubRelatedPersonDetails", clubPers)
Console.WriteLine(pers.ToString())
Output:
{
"PersonId": "abc",
"Firstname": "Max",
"Lastname": "Muster",
"AddressLine1": "Hauptstrasse 3",
"ZipCode": "8000",
"City": "Zürich",
"ClubRelatedPersonDetails": {
"IsGliderInstructor": true,
"IsGliderTrainee": false,
"IsTowPilot": true
}
}
Upvotes: 1