简体   繁体   English

在不知道结构的情况下使用 Newtonsoft.Json 解析 JSON

[英]Parsing JSON Using Newtonsoft.Json Without Knowing the Structure

I'm working on a project that involves automating API calls using a Swagger Definition.我正在开展一个项目,该项目涉及使用 Swagger 定义自动执行 API 调用。 I download the swagger.json file.我下载了 swagger.json 文件。 The structure of the JSON Object I need to parse is not consistent.我需要解析的JSON Object的结构不一致。 When parsing paths, there are a list of objects, then within that they have the methods that can be used for that specific path.解析路径时,有一个对象列表,然后在其中它们具有可用于该特定路径的方法。 I can retrieve just the path using various string methods but my question was, is there a good way to parse json if the JSON is structured in such a way that it does not have a firm key?我可以使用各种字符串方法仅检索路径,但我的问题是,如果 JSON 的结构没有固定密钥,是否有解析 json 的好方法? Here is an example of what I mean:这是我的意思的一个例子:

{"/user": {
    "post": {
      "tags": [
        "user"
      ],
      "summary": "Create user",
      "description": "This can only be done by the logged in user.",
      "operationId": "createUser",
      "consumes": [
        "application/json"
      ],
      "produces": [
        "application/json",
        "application/xml"
      ],
      "parameters": [
        {
          "in": "body",
          "name": "body",
          "description": "Created user object",
          "required": true,
          "schema": {
            "$ref": "#/definitions/User"
          }
        }
      ],
      "responses": {
        "default": {
          "description": "successful operation"
        }
      }
    }
  }

If I wanted to just parse that path and retrieve the method object how could I go about that considering sometimes the object will be "post" or sometimes it will be "get", "put", etc depending on what is allowable for the path.如果我只想解析该路径并检索方法 object 我怎么能 go 考虑到有时 object 将是“发布”,或者有时它将是“允许”的路径,“ .

            JObject jsonResp = swaggerDownload();
            JObject paths = (JObject)jsonResp["paths"];
            foreach (var i in paths)
            {
                string pathToString = i.ToString();
                var shaveSomethings = pathToString.Substring(1, something.Length - 2);
                var pathAndJson = shaveSomethings.Split(new[] { ',' }, 2);
                string correctJsonStructure = "{\"" + pathAndJson[0] + "\":" + pathAndJson[1] + "}";
                JObject bd = JObject.Parse(correctJsonStructure);
                //dynamic pathsTest = JsonConvert.DeserializeObject<dynamic>(correctJsonStructure);
                //JObject result = JsonConvert.DeserializeObject<JObject>(correctJsonStructure);
                //Console.WriteLine(bd["/user"]);

            }

在此处输入图像描述

The swagger.json file should have full definition of each entity that endpoints return. swagger.json 文件应具有端点返回的每个实体的完整定义。 You can follow How to create Rest API client to get a working client.您可以按照如何创建 Rest API 客户端获取工作客户端。

I've dealt with an API where responses didn't always match the definition.我已经处理了一个 API ,其中响应并不总是符合定义。 I saved all responses to a store/log first and then would try to de-serialize JSON.我首先将所有响应保存到存储/日志,然后尝试反序列化 JSON。 In case of an exception I would go back to store/log and see what was different and update my code to accommodate for the change.如果出现异常,我会将 go 重新存储/记录并查看有什么不同并更新我的代码以适应更改。 After few iterations there were no new changes and the ordeal was over.几次迭代后,没有新的变化,磨难结束了。

Hope that helps.希望有帮助。

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

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