Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何查询JSON对象以获取列表_C#_Arrays_Json - Fatal编程技术网

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

C# 如何查询JSON对象以获取列表,c#,arrays,json,C#,Arrays,Json,我有一个JSON文件,如下所示: { "APIs": [ { "name": "ALPHA.API01.10", "scope": "ALPHA", "service": "API01", "version": "1.0.0.0" }, { "name": "ALPHA.API02.12", "scope": "ALPHA", "service"

我有一个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"
      }
]}
我正在尝试获取同一作用域下的服务名称。例如,在scope 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列表。请让我有个更好的主意。

你可以用


你有什么错误吗?如果你不是,我想你只是第一个。这可能是因为您在示例的最后一行中使用了Find。“查找”返回第一个事件,其中将返回符合条件的所有事件。因此,如果没有出现错误,请在何处尝试
  var data = Newtonsoft.Json.JsonConvert.DeserializeObject<APIObject>(jsonString);
  var result = data.APIs.Where(x => x.scope == "ALPHA").Select(x => x.service).ToList();