是的,您可以将列表传递给JObject构造函数,Json.NET将完成其余的工作.这是一个例子:
using System;
using System.Linq;
using Newtonsoft.Json.Linq;
public class Test
{
static void Main()
{
JObject original = JObject.Parse("{ \"x\": \"a\", \"y\": \"b\", \"z\": 1 }");
var properties = original
.Properties()
.Where(p => p.Value.Type == JTokenType.String)
.ToList();
var recreated = new JObject(properties);
Console.WriteLine(recreated);
}
}
输出:
{
"x": "a",
"y": "b"
}
(缺少z属性,因为它的值不是字符串.)