如何查询JSON对象以获取列表

问题描述 投票:1回答:1

我有这样的JSON文件:

 {
 "APIs": [
      {
        "name": "ALPHA.API01.10",
        "scope": "ALPHA",
        "service": "API01",
        "version": "1.0.0.0"
      },
      {
        "name": "ALPHA.API02.12",
        "scope": "ALPHA",
        "service": "API02",
        "version": "1.2.0.3"
      },
      {
        "name": "BETA.API01.10",
        "scope": "BETA",
        "service": "API01",
        "version": "1.0.0.0"
      },
      {
       "name": "BETA.API01.20",
       "scope": "BETA",
       "service": "API01",
       "version": "2.0.0.0"
      }
]}

我正在尝试在相同范围内获得服务名称。例如,在范围ALPHA下,我想获取所有服务名称。

这是我的C#代码:

public class APIs
{
    public string name { get; set; }
    public string scope { get; set; }
    public string service { get; set; }
    public string version { get; set; }
}
public class APIObject
{
    public APIs[] APIs { get; set; }
}


JArray testArray = (JArray)jObject["APIs"];
var query = from testing in testArray
                select new APIs
                    {
                        scope = (string)testing["scope"],
                        service = (string)testing["service"],
                        version = (string)testing["version"]
                    };

List<APIs> testings = query.ToList() as List<APIs>;
var item = testings.Find(f => f.scope == "ALPHA");

使用此代码,我可以在列表对象中获取API的列表。请给我更好的主意。

c# arrays json
1个回答
2
投票

您可以使用JSON.Net

  var data = Newtonsoft.Json.JsonConvert.DeserializeObject<APIObject>(jsonString);
  var result = data.APIs.Where(x => x.scope == "ALPHA").Select(x => x.service).ToList();
© www.soinside.com 2019 - 2024. All rights reserved.