我必须创建一个JSON字符串,最后应该如下所示:
{
"PersonId": "abc",
"Firstname": "Max",
"Lastname": "Muster",
"AddressLine1": "Hauptstrasse 3",
"ZipCode": "8000",
"City": "Zürich",
"ClubRelatedPersonDetails":
{
"IsGliderInstructor": true,
"IsGliderTrainee": false,
"IsTowPilot": true,
}
}
我已经有两个单独的JObjects
:
pers
,其中包含所有姓名和地址信息(上例中的PersonId
到City
)clubPers
,其中包含与俱乐部相关的信息(IsGliderInstructor
到IsTowPilot
)我认为我必须将clubPers
作为JObject
添加到pers
吗?我怎么能在VB.NET中做到这一点?
答案 0 :(得分:1)
只需在第一个JObject
上使用Add
方法,提供所需的属性名称,将其他JObject
作为参数。
例如:
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())
输出:
{
"PersonId": "abc",
"Firstname": "Max",
"Lastname": "Muster",
"AddressLine1": "Hauptstrasse 3",
"ZipCode": "8000",
"City": "Zürich",
"ClubRelatedPersonDetails": {
"IsGliderInstructor": true,
"IsGliderTrainee": false,
"IsTowPilot": true
}
}
答案 1 :(得分:0)
如果Option Strict和Option Explicit已关闭:
Dim pers = JObject.Parse(json1)
Dim clubPers = JObject.Parse(json2)
pers.ClubRelatedPersonDetails = clubPers
Console.WriteLine(pers.ToString())
如果他们开启,请使用Brian Rogers的回答。
奖金:在C#中我们写,
dynamic pers = JObject.Parse(json1);
dynamic clubPers = JObject.Parse(json2);
pers.ClubRelatedPersonDetails = clubPers;