问题
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