使用C#或Jobject或NewtonSoft连接2个JSON字符串

时间:2018-06-21 17:01:48

标签: c# json json.net

您好,我有2个json文件,我想加入他们

{
    "PropertyOne": "PropOne",
    "PropertyTwo": "PropTwo",
    "PropertyThree": "PropThree"
}

{
    "MyObject": {
        "PropertyOne": "PropOne",
        "PropertyTwo": "PropTwo",
        "PropertyThree": "PropThree"
    }
}

我如何加入他们的行列,得到以下结果并成为有效的json?

{
"PropertyOne": "PropOne",
"PropertyTwo": "PropTwo",
"PropertyThree": "PropThree",
"MyObject": {
    "PropertyOne": "PropOne",
    "PropertyTwo": "PropTwo",
  "PropertyThree": "PropThree"
}

}

2 个答案:

答案 0 :(得分:1)

我建议使用Newtonsoft。它可以通过Nuget轻松安装。

string json1 = @"{'PropertyOne': 'PropOne','PropertyTwo': 'PropTwo','PropertyThree': 'PropThree'}";
string json2 = @"{'MyObject': {'PropertyOne': 'PropOne','PropertyTwo': 'PropTwo','PropertyThree': 'PropThree'}}";

JObject j1 = JObject.Parse(json1);
JObject j2 = JObject.Parse(json2);

j1.Merge(j2, new JsonMergeSettings {
    MergeArrayHandling = MergeArrayHandling.Union
});

在这里,您将创建两个JSON字符串对象,并使用JObject解析它们。然后,使用merge方法,可以让Newtonsoft在后台处理它。生成的j1对象具有所需的输出。

答案 1 :(得分:0)

尝试以下事情

Private Sub Cbo_Vendor_AfterUpdate()
   Me.Listbox1.Requery
End Sub

Private Sub Cmd_Done_Click()

'Transfer column 3 of selected items in listbox1 to listbox2
Dim i As Integer

For i = Listbox1.ListCount - 1 To 0 Step -1    'Code works but lists programs in reverse order & does not save to table.
  If Listbox1.Selected(i) = True Then
    Listbox2.AddItem Listbox1.Column(3, i)
  End If
Next I

End Sub

Private Sub Cmd_Save_Click()
    If Me.Dirty Then
        Me.Dirty = False
    End If
End Sub

对我有用