我有一个类似的json字符串:
{
"managedObjects": [
{
"id": "13289",
"name": "xxx"
},
{
"id": "13290",
"name": "yyy"
},
{
"id": "13289",
"name": "xxx"
}]
}
我要解析jobject
JObject obj = JObject.Parse(json)
现在,如何从此数组中删除重复元素?我想从jobject中删除ID的重复元素(此处 id=13289
)。
您不需要为此使用类。
// Get your JObject as you've already shown
var obj = JObject.Parse(json);
// Use LINQ to create a List<JToken> of unique values based on ID
// In this case the first occurence of the ID will be kept, repeats are removed
var unique = obj["managedObjects"].GroupBy(x => x["id"]).Select(x => x.First()).ToList();
// Iterate backwards over the JObject to remove any duplicate keys
for (int i = obj["managedObjects"].Count() - 1; i >= 0; i--)
{
var token = obj["managedObjects"][i];
if (!unique.Contains(token))
{
token.Remove();
}
}
// Re-serialize into JSON
var result = JsonConvert.SerializeObject(obj);
输出:
{
"托管对象":[
{
" id":" 13289",
"名称":" xxx"
},
{
" id":" 13290",
"名称":" yyy"
}
]
}
应保留"名称"以及任何其他节点的其他属性。
我不知道是否有一种直接指定使用json.net的方法,但是解决方案是(如果您的数组的订单无关紧要)确实会读取JSON在具有重载Equals
方法的自定义类结构中:
var obj = JsonConvert.DeserializeObject<Container>(json);
// ...
public class Container
{
public HashSet<ManagedObject> ManagedObjects { get; set;}
}
public class ManagedObject
{
public string Id { get; set; }
public string Name { get; set; }
protected bool Equals(ManagedObject other)
{
return string.Equals(Id, other.Id);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((ManagedObject)obj);
}
public override int GetHashCode()
{
return (Id != null ? Id.GetHashCode() : 0);
}
}
您可以在本地创建托管类型,然后将您的JSON对此类型进行验证。然后,您可以使用LINQ获取不同的列表。这是一个演示此;
的控制台应用程序using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
namespace ConsoleApplication28
{
class Program
{
static void Main(string[] args)
{
var json =
"{rn"managedObjects": [rn{rn "id": "13289", rn "name": "xxx" rn},rn{rn "id": "13290", rn "name": "yyy" rn},rn{rn "id": "13289", rn "name": "xxx" rn}]rn}";
var managedObjectCollection = JsonConvert.DeserializeObject<ManagedObjectCollection>(json);
var distinctManagedObjects = managedObjectCollection.managedObjects.GroupBy(a => a.id).Select(b => b.First());
foreach (var distinctManagedObject in distinctManagedObjects)
{
Console.WriteLine("Id: {0}, Name: {1}", distinctManagedObject.id, distinctManagedObject.name);
}
Console.ReadLine();
}
class ManagedObjectCollection
{
public IEnumerable<ManagedObject> managedObjects { get; set; }
}
class ManagedObject
{
public int id { get; set; }
public string name { get; set; }
}
}
}
我很感谢您提供了一个简化的示例,但是对于一个更复杂的示例,您只需要创建/展开您所挑选的类。
您需要以下类:
public class ManagedObject
{
public string Id { get; set; }
public string Name { get; set; }
}
public class RootObject
{
public List<ManagedObject> ManagedObjects { get; set; }
}
class MyComparer : IEqualityComparer<ManagedObject>
{
public bool Equals(ManagedObject x, ManagedObject y)
{
return x.Id.Equals(y.Id);
}
public int GetHashCode(ManagedObject obj)
{
return obj.Id.GetHashCode();
}
}
然后,您可以像以下内容那样对JSON进行认可:
var test = JsonConvert.DeserializeObject<RootObject>(json);
并从其托管对象列表中删除重复项:
var newManagedObject = test.ManagedObjects.Distinct(new MyComparer()).ToList();
test.ManagedObjects = newManagedObject;
有点混乱。