且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Json.Net(newtonsoft)连接两个Json对象

更新时间:2022-12-27 22:34:09

使用JContainer.Merge().

将JSON对象组合在一起的逻辑非常简单:名称/值被复制,如果现有属性已经具有值,则跳过null.

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.

示例:

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.

请注意,对于两个对象中都存在的属性,jObject2优先(即覆盖jObject1中的属性).

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