如何解决如何使用LINQ中的密钥对值从单个字段中获取重复数据?
这是我下面的JSON
{
"status": "OK","Response": [{
"id": 43,"Name": "name 1","SubName": "Sub 1"
"Link": "Link 1","Active": 1,"Genre": 8,"Description": "Description 1","Languages": "","IsDeleted": 0,"GenreId": 8
},{
"id": 44,"SubName": "Sub 2"
"Link": "Link 2","Genre": 9,"Description": "Description 2","GenreId": 9
},{
"id": 45,"SubName": "Sub 3"
"Link": "Link 3","Genre": 10,"Description": "Description 3","GenreId": 10
},{
"id": 46,"Name": "name 2","SubName": ""
"Link": "Link 4","Genre": 11,"Description": "Description 4","GenreId": 11
},{
"id": 47,"Name": "name 3","SubName": "Sub 1"
"Link": "Link 5","Genre": 12,"Description": "Description 5","GenreId": 12
},{
"id": 48,"SubName": "Sub 2"
"Link": "Link 6","Genre": 13,"Description": "Description 6","GenreId": 13
},{
"id": 49,"SubName": "Sub 3"
"Link": "Link 7","Genre": 14,"Description": "Description 7","GenreId": 14
},{
"id": 50,"SubName": "Sub 4"
"Link": "Link 8","Genre": 15,"Description": "Description 8","GenreId": 15
},{
"id": 51,"Name": "name 4","SubName": ""
"Link": "Link 9","Genre": 16,"Description": "Description 9","GenreId": 16
}]
}
我试图使像下面的JSON
name 1.json
{
"rows": [{
"title": "Sub 1","items": [{
"id": 43,"SubName": "Sub 1"
"Link": "Link 1","GenreId": 8
}]
},{
"title": "Sub 2","items": [{
""id": 44,"SubName": "Sub 2"
"Link": "Link 2","GenreId": 9
}]
},{
"title": "Sub 3","items": [{
""id": 45,"SubName": "Sub 3"
"Link": "Link 3","Genre": 10
"Description": "Description 3","GenreId": 10
}]
}
}]
}
name 3.json
{
"rows": [{
"title": "Sub 1","items": [{
"id": 47,"SubName": "Sub 1"
"Link": "Link 5","GenreId": 12
}]
},"items": [{
""id": 48,"SubName": "Sub 2"
"Link": "Link 6","GenreId": 13
}]
},"items": [{
""id": 49,"SubName": "Sub 3"
"Link": "Link 7","Genre": 14
"Description": "Description 7","GenreId": 14
}]
},{
"title": "Sub 4","items": [{
""id": 50,"SubName": "Sub 4"
"Link": "Link 8","Genre": 15
"Description": "Description 8","GenreId": 15
}]
}
}]
}
我正在尝试使用重复名称明智地分隔一个JSON,如上面。我在JSON中只找到重复的名称,而没有其他内容。
有人知道如何获取重复值吗?
我尝试使用Linq查询并使用
mygeneratedresponse.Skip(1).ToList();
正在工作。但是有更好的解决方案吗?
解决方法
我不确定这是否是您要查找的内容,但是,请采用一种方法来获取重复值,并使用linq将其分组;
您只需要创建一个consoleapp并将newtonsoft.json添加到其中,即可运行以下代码。
class Program
{
static string pathA = "<place-your-first-json-location-here>"; //ex: c:\test\test.json
static void Main(string[] args)
{
var json = JsonConvert.DeserializeObject<Test>(File.ReadAllText(pathA));
var filteredByName = json.Response.GroupBy(g => g.Name).OrderBy(o => o.Key).ToList();
foreach (var item in filteredByName)
{
Console.WriteLine($"Name: {item.Key} Qty response: {item.Count()}" );
}
}
}
public class Test {
public string Status { get; set; }
public List<Response> Response { get; set; }
}
public class Response {
public int Id { get; set; }
public string Name { get; set; }
public string SubName { get; set; }
public string Link { get; set; }
public int Active { get; set; }
public int Genre { get; set; }
public string Description { get; set; }
public string Languages { get; set; }
public int IsDeleted { get; set; }
public int GenreId { get; set; }
}
这是预期的输出,因此您可以继续将其应用于逻辑;
Name: name 1 Qty response: 3
Name: name 2 Qty response: 1
Name: name 3 Qty response: 4
Name: name 4 Qty response: 1
,
以下代码将生成您的预期输出: 此代码也位于.net小提琴https://dotnetfiddle.net/d2uSNC
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Linq;
public class Program
{
public static void Main()
{
var jsonString = "{ \"status\": \"OK\",\"Response\": [{ \"id\": 43,\"Name\": \"name 1\",\"SubName\": \"Sub 1\",\"Link\": \"Link 1\",\"Active\": 1,\"Genre\": 8,\"Description\": \"Description 1\",\"Languages\": \"\",\"IsDeleted\": 0,\"GenreId\": 8 },{ \"id\": 44,\"SubName\": \"Sub 2\",\"Link\": \"Link 2\",\"Genre\": 9,\"Description\": \"Description 2\",\"GenreId\": 9 },{ \"id\": 45,\"SubName\": \"Sub 3\",\"Link\": \"Link 3\",\"Genre\": 10,\"Description\": \"Description 3\",\"GenreId\": 10 },{ \"id\": 46,\"Name\": \"name 2\",\"SubName\": \"\",\"Link\": \"Link 4\",\"Genre\": 11,\"Description\": \"Description 4\",\"GenreId\": 11 },{ \"id\": 47,\"Name\": \"name 3\",\"Link\": \"Link 5\",\"Genre\": 12,\"Description\": \"Description 5\",\"GenreId\": 12 },{ \"id\": 48,\"Link\": \"Link 6\",\"Genre\": 13,\"Description\": \"Description 6\",\"GenreId\": 13 },{ \"id\": 49,\"Link\": \"Link 7\",\"Genre\": 14,\"Description\": \"Description 7\",\"GenreId\": 14 },{ \"id\": 50,\"SubName\": \"Sub 4\",\"Link\": \"Link 8\",\"Genre\": 15,\"Description\": \"Description 8\",\"GenreId\": 15 },{ \"id\": 51,\"Name\": \"name 4\",\"Link\": \"Link 9\",\"Genre\": 16,\"Description\": \"Description 9\",\"GenreId\": 16 }]}";
var json = JsonConvert.DeserializeObject<Root>(jsonString);
var filteredByName = json.Response.GroupBy(g => g.Name).OrderBy(o => o.Key).ToList();
foreach (var item in filteredByName)
{
var fileObject = item.GroupBy(x=>x.SubName).Select(s=> new { title =s.Key,items= s.ToList()});
var fileJson = JsonConvert.SerializeObject(fileObject,Formatting.Indented);
Console.WriteLine("" + item.Key +".json");
Console.WriteLine(fileJson);
}
}
}
public class JsonResponse {
public int id { get; set; }
public string Name { get; set; }
public string SubName { get; set; }
public string Link { get; set; }
public int Active { get; set; }
public int Genre { get; set; }
public string Description { get; set; }
public string Languages { get; set; }
public int IsDeleted { get; set; }
public int GenreId { get; set; }
}
public class Root {
public string status { get; set; }
public List<JsonResponse> Response { get; set; }
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。