How do I concatenate two Json Objects using Json.Net (newtonsoft) [duplicate]

我们两清 提交于 2021-02-07 10:19:41

问题


I have JSON objects that I want to concatenate into one JSON object.

How do I do that using NewtonSoft's JSON package?


回答1:


Use JContainer.Merge().

The logic for combining JSON objects together is fairly simple: name/values are copied across, skipping nulls if the existing property already has a value.

Json.NET 6.0 Release 4

Example:

var jObject1 = // Your first json object as JObject
var jObject2 = // Your second json object as JObject 

jObject1.Merge(jObject2);

// jObject1 contains now the merged properties from jObject2.

Note that for properties that exist in both objects, the jObject2 ones take precedence (i.e. overwrite the properties in jObject1).



来源:https://stackoverflow.com/questions/26222045/how-do-i-concatenate-two-json-objects-using-json-net-newtonsoft

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!