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

标签 c# json.net

<分区>

我有一些 JSON 对象,我想将它们连接成一个 JSON 对象。

我如何使用 NewtonSoft 的 JSON 包来做到这一点?

最佳答案

使用 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

例子:

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 中的属性)。

关于c# - 如何使用 Json.Net (newtonsoft) 连接两个 Json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26222045/

相关文章:

c# - 无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List` 1

c# - .net 特定于文化的 12/24 小时格式

c# - 什么是 XAML 在未处理的异常和 app.g.i.cs 文件上生成中断

c# - linq 按 2 个条件计数

c# - 使用超链接控件从数据库中检索数据

c# - 什么是消息泵?

Json.NET 和 GC 等待的 C# 服务性能问题

c# - Json转换器 : Return a single object or a List<Object> based on inbound JSON

asp.net-mvc - 在 MVC4 项目中使用 Json.NET 序列化器

c# - 从 JsonReader 读取 JObject 时出错。当前 JsonReader 项不是对象 : StartArray. 路径