【问题标题】:Access a specific property in deeply nested json using c# and add it to an array使用 c# 访问深度嵌套的 json 中的特定属性并将其添加到数组中
【发布时间】:2020-06-26 21:48:51
【问题描述】:

我正在尝试访问以下 json 中 OperationId 的所有值并将其添加到数组中,但是当我深入研究数据时,它一直给我一个空异常。下面是 json,任何关于我如何直接访问 operationIds 的建议都非常感谢。

注意:所有控制器名称和 HttpVerbs 在整个 json 中都是可变的,这只是 10,000 行 Json 文件的一小部分,因此硬编码每个值以获取字符串是行不通的。

json:

{
    "swagger": "2.0",
    "info": {
        "version": "v1",
        "title": "API Services"
    },
    "host": "TestApi.Com",
    "basePath": "/TestApi",
    "schemes": ["https"],
    "paths": {
        "/activity/actions": {
            "get": {
                "tags": ["activities"],
                "summary": "Returns a list of non-hidden Actions.",
                "description": "Test Description",
                ***"operationId": "GET/activity/actions",  //Need to add this to array***
                "consumes": [],
                "produces": ["application/json", "text/json", "application/xml", "text/xml"],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "type": "array",
                            "items": {
                                "$ref": "#/definitions/ActionResponse"
                            }
                        }
                    }
                }
            },
            "put": {
                "tags": ["activities"],
                "summary": "Updates any/all existing ActivityActions by matching Id",
                "description": "Test Description",
                ***"operationId": "PUT/activity/actions", //Need to add this to array***
                "consumes": ["application/json", "text/json", "application/xml", "text/xml", "application/x-www-form-urlencoded"],
                "produces": ["application/json", "text/json", "application/xml", "text/xml"],
                "parameters": [{
                    "name": "List`1",
                    "in": "body",
                    "required": true,
                    "schema": {
                        "type": "array",
                        "items": {
                            "$ref": "#/definitions/ActivityActionPutRequest"
                        }
                    }
                }, {
                    "name": "Authorization",
                    "in": "header",
                    "description": "AccessToken",
                    "required": false,
                    "type": "string"
                }],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "type": "array",
                            "items": {
                                "$ref": "#/definitions/ActivityActionUpdateResponse"
                            }
                        }
                    }
                }
            }
        },
        "/activity/agency-url/create": {
            "get": {
                "tags": ["activities"],
                "summary": "Test",
                ***"operationId": "GET/activity/agency-url/create",  //Need to add this to array***
                "consumes": [],
                "produces": ["application/json", "text/json", "application/xml", "text/xml"],
                "parameters": [{
                    "name": "EntityType",
                    "in": "query",
                    "description": "Test",
                    "required": true,
                    "type": "string"
                }, {
                    "name": "EntityId",
                    "in": "query",
                    "description": "The Guid or Sequence No of the entity.",
                    "required": true,
                    "type": "string"
                }, {
                    "name": "Authorization",
                    "in": "header",
                    "description": "AccessToken",
                    "required": false,
                    "type": "string"
                }],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/ActivityUrlResponse"
                        }
                    }
                }
            }
        }
    }
}

所以输出看起来像:

[GET/activity/actions, PUT/activity/actions, GET/activity/agency-url/create]

【问题讨论】:

    标签: c# json json.net json-deserialization


    【解决方案1】:

    首先定义 JSON 结构 - 这只是读取所需字段所需的最小结构:

    public class RootJson
    {
        public Paths paths { get; set; }
    }
    
    public class Paths : Dictionary<string, ActivityActions> { }
    
    public class ActivityActions
    {
        public Get get { get; set; }
        public Put put { get; set; }
    }
    
    public class Get
    {
        public string operationId { get; set; }
    }
    
    public class Put
    {
        public string operationId { get; set; }
    }
    

    然后像这样提取数据(我正在阅读所有获取然后所有放置,您可能需要不同的排序):

    var json = "{ ... define your json }";
    var test = JsonConvert.DeserializeObject<RootJson>(json);
    
    var result = new List<string>();
    result.AddRange(test.paths.Select(p => p.Value.get?.operationId).Where(oid => oid != null));
    result.AddRange(test.paths.Select(p => p.Value.put?.operationId).Where(oid => oid != null));
    

    现在结果包含你想要的列表。

    【讨论】:

      【解决方案2】:

      所以我会使用Newtonsoft的方法。

      类似这样的:

      private static IEnumerable<JToken> GetToFindPropertiesAsJTokens(string json)
              {
                  IEnumerable<JToken> jTokens = null;
                  JObject jObject = JObject.Parse(json);
                  jTokens = jObject.Descendants()
                      .Where(t => t.Type == JTokenType.Property && ((JProperty)t).Name == "operationId")
                      .Select(p => ((JProperty)p).Value);
                  return jTokens;
              }
      

      使用:

       List<string> values = new List<string>();
       foreach (JToken jtoken in GetToFindPropertiesAsJTokensBasedOnTable(json))
       {
           values.Add(jtoken.ToString());
       }
      
       string[] valuesArray = values.ToArray();
      
       Array.ForEach(valuesArray, x=> Console.WriteLine(x));
      

      输出:

      GET/activity/actions
      PUT/activity/actions
      GET/activity/agency-url/create
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-04
        • 1970-01-01
        • 2023-03-09
        • 1970-01-01
        • 2014-06-10
        • 2018-11-16
        • 2020-06-15
        • 2018-01-03
        相关资源
        最近更新 更多