C# 我想查询一个json文件,以查找具有两个或更多共同标记的用户
我有以下json文件:C# 我想查询一个json文件,以查找具有两个或更多共同标记的用户,c#,json,linq,json.net,C#,Json,Linq,Json.net,我有以下json文件: { “用户”:[ { “标签”:[ “c++”, “jquery”, “css”, “html” ], “姓名”:“约翰·多伊”, “id”:0 }, { “标签”:[ “vb”, “css” ], “姓名”:“比尔·盖茨”, “id”:1 }, { “标签”:[ “c++”, “css”, “html” ], “姓名”:“史蒂夫·乔布斯”, “id”:3 } ] } 我试图只返回匹配标记两次或两次以上的用户,例如: 某个身份不明的人和史蒂夫·乔布斯有C++和CSS的共
{
“用户”:[
{
“标签”:[
“c++”,
“jquery”,
“css”,
“html”
],
“姓名”:“约翰·多伊”,
“id”:0
},
{
“标签”:[
“vb”,
“css”
],
“姓名”:“比尔·盖茨”,
“id”:1
},
{
“标签”:[
“c++”,
“css”,
“html”
],
“姓名”:“史蒂夫·乔布斯”,
“id”:3
}
]
}
我试图只返回匹配标记两次或两次以上的用户,例如:
<>某个身份不明的人和史蒂夫·乔布斯有C++和CSS的共同点。
我试图通过执行大量for语句来实现这一点,但我认为这不是最好的解决方案
这就是我到目前为止的情况:
JObject res=JObject.Parse(File.ReadAllText(@“C:/json/data.json”);
int jsonLength=res[“用户”].Count();
for(int i=0;i
我看到一些人像在SQL中一样查询json文件,但我以前从未使用过LINQ,所以我不知道它是如何工作的,我也不确定什么是最好的方法 您应该根据发布的JSON(使用)创建以下模型。将JSON字符串反序列化为
RootObject
,然后可以使用LINQ查询根据需要过滤数据
public class User
{
public List<string> tags { get; set; }
public string name { get; set; }
public int id { get; set; }
}
public class RootObject
{
public List<User> users { get; set; }
}
公共类用户
{
公共列表标记{get;set;}
公共字符串名称{get;set;}
公共int id{get;set;}
}
公共类根对象
{
公共列表用户{get;set;}
}
您应该根据发布的JSON(使用)创建以下模型。将JSON字符串反序列化为RootObject
,然后可以使用LINQ查询根据需要过滤数据
public class User
{
public List<string> tags { get; set; }
public string name { get; set; }
public int id { get; set; }
}
public class RootObject
{
public List<User> users { get; set; }
}
公共类用户
{
公共列表标记{get;set;}
公共字符串名称{get;set;}
公共int id{get;set;}
}
公共类根对象
{
公共列表用户{get;set;}
}
创建一个用户
类:
public class User
{
public IEnumerable<string> Tags { get; set; }
public string Name { get; set; }
public int Id { get; set; }
}
公共类用户
{
公共IEnumerable标记{get;set;}
公共字符串名称{get;set;}
公共int Id{get;set;}
}
然后创建一个类来保存JSON数据:
public class ResponseData
{
public IEnumerable<User> Users { get; set; }
}
公共类响应数据
{
公共IEnumerable用户{get;set;}
}
现在继续反序列化JSON:
string json = "....";
ResponseData data = JsonConvert.DeserializeJson<ResponseData>(json);
IEnumerable<User> users = data.Users;
string json=“…”;
ResponseData=JsonConvert.DeserializeJson(json);
IEnumerable users=data.users;
然后,要查找具有公共标记的用户,可以创建一个扩展方法:
public static IEnumerable<User> WithTag(this IEnumerable<User> users, string tag)
{
if (users == null) return null;
return users.Where(u => u.Tags.Contains(tag));
}
公共静态IEnumerable with标记(此IEnumerable用户,字符串标记)
{
如果(users==null)返回null;
返回users.Where(u=>u.Tags.Contains(tag));
}
您可以这样调用该方法:
IEnumerable<User> users = data.Users;
IEnumerable<User> cppGroup = users.WithTag("c++");
IEnumerable<User> users = data.Users;
IDictionary<string, IEnumerable<User>> commonTags = users.AllCommonTags();
IEnumerable<User> cppGroup = tags["c++"];
IEnumerable<User> htmlGroup = tags["html"];
string json = File.ReadAllText(@"C:\json\data.json");
JArray users = (JArray)JObject.Parse(json)["users"];
// Generate pair-wise combinations of users
// and check for intersection of their tags.
// If two or more common tags, add both users to a hash set
HashSet<JObject> result = new HashSet<JObject>();
for (int i = 0; i < users.Count; i++)
{
JObject user1 = (JObject)users[i];
for (int j = i + 1; j < users.Count; j++)
{
JObject user2 = (JObject)users[j];
if (user1["tags"].Select(t => t.ToString())
.Intersect(user2["tags"].Select(t => t.ToString()))
.Count() > 1)
{
result.Add(user1);
result.Add(user2);
}
}
}
Console.WriteLine("All users that share two or more tags with another user:");
Console.WriteLine();
foreach (JObject user in result)
{
Console.WriteLine("id: " + user["id"]);
Console.WriteLine("name: " + user["name"]);
Console.WriteLine("tags: " + string.Join(", ", user["tags"]));
Console.WriteLine();
}
string json = File.ReadAllText(@"C:\json\data.json");
// Parse the JSON into a list of Users
List<User> users = JObject.Parse(json)["users"]
.Select(t => t.ToObject<User>())
.ToList();
// Generate pair-wise combinations of users
// and check for intersection of their tags.
// If two or more common tags, add the pairing to a list
List<Pairing> pairings = new List<Pairing>();
for (int i = 0; i < users.Count; i++)
{
User user1 = users[i];
for (int j = i + 1; j < users.Count; j++)
{
User user2 = users[j];
var commonTags = user1.Tags.Intersect(user2.Tags).ToList();
if (commonTags.Count > 1)
{
pairings.Add(new Pairing
{
User1 = user1,
User2 = user2,
CommonTags = commonTags
});
}
}
}
// Write out the results
Console.WriteLine("Pairs of users sharing two or more tags with each other:");
Console.WriteLine();
foreach (Pairing p in pairings)
{
Console.WriteLine(string.Format("{0} (id {1}) and {2} (id {3}) have ({4}) in common.",
p.User1.Name, p.User1.Id, p.User2.Name, p.User2.Id, string.Join(", ", p.CommonTags)));
}
IEnumerable users=data.users;
IEnumerable cppGroup=users.WithTag(“c++”);
如果要获取所有可能的标记:
public static IEnumerable<string> AllTags(this IEnumerable<User> users)
{
if (users == null) return null;
return users.Select(u => u.Tags).SelectMany(t => t).Distinct();
}
公共静态IEnumerable AllTags(此IEnumerable用户)
{
如果(users==null)返回null;
返回users.Select(u=>u.Tags).SelectMany(t=>t.Distinct();
}
最后,如果要获取所有标记的所有公共用户:
public static IDictionary<string, IEnumerable<User>> AllCommonTags(this IEnumerable<User> users)
{
if (users == null) return null;
return users.AllTags().Select(t => new
{
Tag = t, Users = users.WithTag(t)
}).ToDictionary(ct => ct.Tag, ct => ct.Users);
}
公共静态IDictionary AllCommonTags(此IEnumerable用户)
{
如果(users==null)返回null;
return users.AllTags().Select(t=>new
{
Tag=t,Users=Users.WithTag(t)
}).ToDictionary(ct=>ct.Tag,ct=>ct.Users);
}
你会这样消费它:
IEnumerable<User> users = data.Users;
IEnumerable<User> cppGroup = users.WithTag("c++");
IEnumerable<User> users = data.Users;
IDictionary<string, IEnumerable<User>> commonTags = users.AllCommonTags();
IEnumerable<User> cppGroup = tags["c++"];
IEnumerable<User> htmlGroup = tags["html"];
string json = File.ReadAllText(@"C:\json\data.json");
JArray users = (JArray)JObject.Parse(json)["users"];
// Generate pair-wise combinations of users
// and check for intersection of their tags.
// If two or more common tags, add both users to a hash set
HashSet<JObject> result = new HashSet<JObject>();
for (int i = 0; i < users.Count; i++)
{
JObject user1 = (JObject)users[i];
for (int j = i + 1; j < users.Count; j++)
{
JObject user2 = (JObject)users[j];
if (user1["tags"].Select(t => t.ToString())
.Intersect(user2["tags"].Select(t => t.ToString()))
.Count() > 1)
{
result.Add(user1);
result.Add(user2);
}
}
}
Console.WriteLine("All users that share two or more tags with another user:");
Console.WriteLine();
foreach (JObject user in result)
{
Console.WriteLine("id: " + user["id"]);
Console.WriteLine("name: " + user["name"]);
Console.WriteLine("tags: " + string.Join(", ", user["tags"]));
Console.WriteLine();
}
string json = File.ReadAllText(@"C:\json\data.json");
// Parse the JSON into a list of Users
List<User> users = JObject.Parse(json)["users"]
.Select(t => t.ToObject<User>())
.ToList();
// Generate pair-wise combinations of users
// and check for intersection of their tags.
// If two or more common tags, add the pairing to a list
List<Pairing> pairings = new List<Pairing>();
for (int i = 0; i < users.Count; i++)
{
User user1 = users[i];
for (int j = i + 1; j < users.Count; j++)
{
User user2 = users[j];
var commonTags = user1.Tags.Intersect(user2.Tags).ToList();
if (commonTags.Count > 1)
{
pairings.Add(new Pairing
{
User1 = user1,
User2 = user2,
CommonTags = commonTags
});
}
}
}
// Write out the results
Console.WriteLine("Pairs of users sharing two or more tags with each other:");
Console.WriteLine();
foreach (Pairing p in pairings)
{
Console.WriteLine(string.Format("{0} (id {1}) and {2} (id {3}) have ({4}) in common.",
p.User1.Name, p.User1.Id, p.User2.Name, p.User2.Id, string.Join(", ", p.CommonTags)));
}
IEnumerable users=data.users;
IDictionary commonTags=users.AllCommonTags();
IEnumerable cppGroup=标记[“c++];
IEnumerable htmlGroup=标记[“html”];
创建一个用户
类:
public class User
{
public IEnumerable<string> Tags { get; set; }
public string Name { get; set; }
public int Id { get; set; }
}
公共类用户
{
公共IEnumerable标记{get;set;}
公共字符串名称{get;set;}
公共int Id{get;set;}
}
然后创建一个类来保存JSON数据:
public class ResponseData
{
public IEnumerable<User> Users { get; set; }
}
公共类响应数据
{
公共IEnumerable用户{get;set;}
}
现在继续反序列化JSON:
string json = "....";
ResponseData data = JsonConvert.DeserializeJson<ResponseData>(json);
IEnumerable<User> users = data.Users;
string json=“…”;
ResponseData=JsonConvert.DeserializeJson(json);
IEnumerable users=data.users;
然后,要查找具有公共标记的用户,可以创建一个扩展方法:
public static IEnumerable<User> WithTag(this IEnumerable<User> users, string tag)
{
if (users == null) return null;
return users.Where(u => u.Tags.Contains(tag));
}
公共静态IEnumerable with标记(此IEnumerable用户,字符串标记)
{
如果(users==null)返回null;
返回users.Where(u=>u.Tags.Contains(tag));
}
您可以这样调用该方法:
IEnumerable<User> users = data.Users;
IEnumerable<User> cppGroup = users.WithTag("c++");
IEnumerable<User> users = data.Users;
IDictionary<string, IEnumerable<User>> commonTags = users.AllCommonTags();
IEnumerable<User> cppGroup = tags["c++"];
IEnumerable<User> htmlGroup = tags["html"];
string json = File.ReadAllText(@"C:\json\data.json");
JArray users = (JArray)JObject.Parse(json)["users"];
// Generate pair-wise combinations of users
// and check for intersection of their tags.
// If two or more common tags, add both users to a hash set
HashSet<JObject> result = new HashSet<JObject>();
for (int i = 0; i < users.Count; i++)
{
JObject user1 = (JObject)users[i];
for (int j = i + 1; j < users.Count; j++)
{
JObject user2 = (JObject)users[j];
if (user1["tags"].Select(t => t.ToString())
.Intersect(user2["tags"].Select(t => t.ToString()))
.Count() > 1)
{
result.Add(user1);
result.Add(user2);
}
}
}
Console.WriteLine("All users that share two or more tags with another user:");
Console.WriteLine();
foreach (JObject user in result)
{
Console.WriteLine("id: " + user["id"]);
Console.WriteLine("name: " + user["name"]);
Console.WriteLine("tags: " + string.Join(", ", user["tags"]));
Console.WriteLine();
}
string json = File.ReadAllText(@"C:\json\data.json");
// Parse the JSON into a list of Users
List<User> users = JObject.Parse(json)["users"]
.Select(t => t.ToObject<User>())
.ToList();
// Generate pair-wise combinations of users
// and check for intersection of their tags.
// If two or more common tags, add the pairing to a list
List<Pairing> pairings = new List<Pairing>();
for (int i = 0; i < users.Count; i++)
{
User user1 = users[i];
for (int j = i + 1; j < users.Count; j++)
{
User user2 = users[j];
var commonTags = user1.Tags.Intersect(user2.Tags).ToList();
if (commonTags.Count > 1)
{
pairings.Add(new Pairing
{
User1 = user1,
User2 = user2,
CommonTags = commonTags
});
}
}
}
// Write out the results
Console.WriteLine("Pairs of users sharing two or more tags with each other:");
Console.WriteLine();
foreach (Pairing p in pairings)
{
Console.WriteLine(string.Format("{0} (id {1}) and {2} (id {3}) have ({4}) in common.",
p.User1.Name, p.User1.Id, p.User2.Name, p.User2.Id, string.Join(", ", p.CommonTags)));
}
IEnumerable users=data.users;
IEnumerable cppGroup=users.WithTag(“c++”);
如果要获取所有可能的标记:
public static IEnumerable<string> AllTags(this IEnumerable<User> users)
{
if (users == null) return null;
return users.Select(u => u.Tags).SelectMany(t => t).Distinct();
}
公共静态IEnumerable AllTags(此IEnumerable用户)
{
如果(users==null)返回null;
返回users.Select(u=>u.Tags).SelectMany(t=>t.Distinct();
}
最后,如果要获取所有标记的所有公共用户:
public static IDictionary<string, IEnumerable<User>> AllCommonTags(this IEnumerable<User> users)
{
if (users == null) return null;
return users.AllTags().Select(t => new
{
Tag = t, Users = users.WithTag(t)
}).ToDictionary(ct => ct.Tag, ct => ct.Users);
}
公共静态IDictionary AllCommonTags(此IEnumerable用户)
{
如果(users==null)返回null;
return users.AllTags().Select(t=>new
{
Tag=t,Users=Users.WithTag(t)
}).ToDictionary(ct=>ct.Tag,ct=>ct.Users);
}
你会这样消费它:
IEnumerable<User> users = data.Users;
IEnumerable<User> cppGroup = users.WithTag("c++");
IEnumerable<User> users = data.Users;
IDictionary<string, IEnumerable<User>> commonTags = users.AllCommonTags();
IEnumerable<User> cppGroup = tags["c++"];
IEnumerable<User> htmlGroup = tags["html"];
string json = File.ReadAllText(@"C:\json\data.json");
JArray users = (JArray)JObject.Parse(json)["users"];
// Generate pair-wise combinations of users
// and check for intersection of their tags.
// If two or more common tags, add both users to a hash set
HashSet<JObject> result = new HashSet<JObject>();
for (int i = 0; i < users.Count; i++)
{
JObject user1 = (JObject)users[i];
for (int j = i + 1; j < users.Count; j++)
{
JObject user2 = (JObject)users[j];
if (user1["tags"].Select(t => t.ToString())
.Intersect(user2["tags"].Select(t => t.ToString()))
.Count() > 1)
{
result.Add(user1);
result.Add(user2);
}
}
}
Console.WriteLine("All users that share two or more tags with another user:");
Console.WriteLine();
foreach (JObject user in result)
{
Console.WriteLine("id: " + user["id"]);
Console.WriteLine("name: " + user["name"]);
Console.WriteLine("tags: " + string.Join(", ", user["tags"]));
Console.WriteLine();
}
string json = File.ReadAllText(@"C:\json\data.json");
// Parse the JSON into a list of Users
List<User> users = JObject.Parse(json)["users"]
.Select(t => t.ToObject<User>())
.ToList();
// Generate pair-wise combinations of users
// and check for intersection of their tags.
// If two or more common tags, add the pairing to a list
List<Pairing> pairings = new List<Pairing>();
for (int i = 0; i < users.Count; i++)
{
User user1 = users[i];
for (int j = i + 1; j < users.Count; j++)
{
User user2 = users[j];
var commonTags = user1.Tags.Intersect(user2.Tags).ToList();
if (commonTags.Count > 1)
{
pairings.Add(new Pairing
{
User1 = user1,
User2 = user2,
CommonTags = commonTags
});
}
}
}
// Write out the results
Console.WriteLine("Pairs of users sharing two or more tags with each other:");
Console.WriteLine();
foreach (Pairing p in pairings)
{
Console.WriteLine(string.Format("{0} (id {1}) and {2} (id {3}) have ({4}) in common.",
p.User1.Name, p.User1.Id, p.User2.Name, p.User2.Id, string.Join(", ", p.CommonTags)));
}
IEnumerable users=data.users;
IDictionary commonTags=users.AllCommonTags();
IEnumerable cppGroup=标记[“c++];
IEnumerable htmlGroup=标记[“html”];
如果您只需要与其他用户共享两个或多个标签的所有用户的列表,可以执行以下操作:
IEnumerable<User> users = data.Users;
IEnumerable<User> cppGroup = users.WithTag("c++");
IEnumerable<User> users = data.Users;
IDictionary<string, IEnumerable<User>> commonTags = users.AllCommonTags();
IEnumerable<User> cppGroup = tags["c++"];
IEnumerable<User> htmlGroup = tags["html"];
string json = File.ReadAllText(@"C:\json\data.json");
JArray users = (JArray)JObject.Parse(json)["users"];
// Generate pair-wise combinations of users
// and check for intersection of their tags.
// If two or more common tags, add both users to a hash set
HashSet<JObject> result = new HashSet<JObject>();
for (int i = 0; i < users.Count; i++)
{
JObject user1 = (JObject)users[i];
for (int j = i + 1; j < users.Count; j++)
{
JObject user2 = (JObject)users[j];
if (user1["tags"].Select(t => t.ToString())
.Intersect(user2["tags"].Select(t => t.ToString()))
.Count() > 1)
{
result.Add(user1);
result.Add(user2);
}
}
}
Console.WriteLine("All users that share two or more tags with another user:");
Console.WriteLine();
foreach (JObject user in result)
{
Console.WriteLine("id: " + user["id"]);
Console.WriteLine("name: " + user["name"]);
Console.WriteLine("tags: " + string.Join(", ", user["tags"]));
Console.WriteLine();
}
string json = File.ReadAllText(@"C:\json\data.json");
// Parse the JSON into a list of Users
List<User> users = JObject.Parse(json)["users"]
.Select(t => t.ToObject<User>())
.ToList();
// Generate pair-wise combinations of users
// and check for intersection of their tags.
// If two or more common tags, add the pairing to a list
List<Pairing> pairings = new List<Pairing>();
for (int i = 0; i < users.Count; i++)
{
User user1 = users[i];
for (int j = i + 1; j < users.Count; j++)
{
User user2 = users[j];
var commonTags = user1.Tags.Intersect(user2.Tags).ToList();
if (commonTags.Count > 1)
{
pairings.Add(new Pairing
{
User1 = user1,
User2 = user2,
CommonTags = commonTags
});
}
}
}
// Write out the results
Console.WriteLine("Pairs of users sharing two or more tags with each other:");
Console.WriteLine();
foreach (Pairing p in pairings)
{
Console.WriteLine(string.Format("{0} (id {1}) and {2} (id {3}) have ({4}) in common.",
p.User1.Name, p.User1.Id, p.User2.Name, p.User2.Id, string.Join(", ", p.CommonTags)));
}
string json=File.ReadAllText(@“C:\json\data.json”);
JArray用户=(JArray)JObject.Parse(json)[“用户”];
//生成用户的成对组合
//并检查它们的标记是否相交。
//如果有两个或多个公共标记,请将两个用户添加到哈希集中
HashSet result=新的HashSet();
for(int)