简体   繁体   English

Linq查询到Json字符串

[英]Linq query to Json string

starting from a JObject I can get the array that interests me: 从JObject开始,我可以获得感兴趣的数组:

JArray partial = (JArray)rssAlbumMetadata["tracks"]["items"];

First question: "partial" contains a lot of attributes I'm not interested on. 第一个问题:“部分”包含许多我不感兴趣的属性。 How can I get only what I need? 我怎样才能只得到我需要的东西?

Second question: once succeeded in the first task I'll get a JArray of duplicated items. 第二个问题:成功完成第一个任务后,我将获得一个JArray重复项。 How can I get only the unique ones ? 我怎样才能只得到独特的? The result should be something like 结果应该是这样的

{
'composer': [
                {
                'id': '51523',
                'name': 'Modest Mussorgsky'
                },
                {
                'id': '228918',
                'name': 'Sergey Prokofiev'
                },
        ]
}

Let me start from something like: 让我从类似的内容开始:

[
  {
    "id": 32837732,
    "composer": {
      "id": 245,
      "name": "George Gershwin"
    },
    "title": "Of Thee I Sing: Overture (radio version)"
  },
  {
    "id": 32837735,
    "composer": {
      "id": 245,
      "name": "George Gershwin"
    },
    "title": "Concerto in F : I. Allegro"
  },
  {
    "id": 32837739,
    "composer": {
      "id": 245,
      "name": "George Gershwin"
    },
    "title": "Concerto in F : II. Adagio"
  }
]

First question: 第一个问题:

How can I get only what I need? 我怎样才能只得到我需要的东西?

There is no magic, you need to read the whole JSON string and then query the object to find what you are looking for. 没有魔术,您需要读取整个JSON字符串,然后查询对象以查找所需内容。 It is not possible to read part of the JSON if that is what you need. 如果您需要的话,则无法读取JSON的一部分。 You have not provided an example of what the data looks like so not possible to specify how to query. 您没有提供数据外观的示例,因此无法指定如何查询。

Second question which I guess is: How to de-duplicate contents of an array of object? 我猜的第二个问题是:如何对对象数组的内容进行重复数据删除?

Again, I do not have full view of your objects but this example should be able to show you - using Linq as you requested: 同样,我没有您的对象的完整视图,但是此示例应该可以向您展示-根据您的要求使用Linq:

var items = new []{new {id=1, name="ali"}, new {id=2, name="ostad"}, new {id=1, name="ali"}};
var dedup = items.GroupBy(x=> x.id).Select(y => y.First()).ToList();
Console.WriteLine(dedup);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM