【问题标题】:Not able to access json file content [duplicate]无法访问json文件内容[重复]
【发布时间】:2019-02-04 09:19:47
【问题描述】:

我正在使用 json 解析器对象解析 json 文件。我无法访问请求结构及其内部正文内容。 我写过这样的代码:

private static final String filePath = "D:\\score_api.json";


public static void main(String[] args) throws FileNotFoundException, IOException, ParseException {

            FileReader reader = new FileReader(filePath);

            JSONParser jsonParser = new JSONParser();
            JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);


            System.out.println("++++++++++++++++++\n"+jsonObject);
            // prints the whole json content. success!

            JSONObject structure = (JSONObject) jsonObject.get("provider");
            System.out.println("provider name: " + structure.get("name"));
            // prints the provider name. success!


            JSONArray req= (JSONArray) jsonObject.get("interactions");
            Iterator i = req.iterator();

           while (i.hasNext()) {
                JSONObject reqObj = (JSONObject) i.next();
                System.out.println("description: " + reqObj.get("description") +"\n");
                System.out.println("request body: " + reqObj.get("request")); // prints full request body 
                System.out.println("path: " + reqObj.get("path") +"\n"); // Failing, getting null value.
                System.out.println("reponse body: " + reqObj.get("response") +"\n"); // Success
           }
        }

它的输出:

++++++++++++++++++++ {"完整的 json 文件内容打印"}
提供者名称:SIS 描述:API POST 分数 请求正文:{“完整的请求正文打印”} 路径:空 响应正文:{"status":200}

我正在努力访问请求正文内容。及其子部分。 我想访问 'path' 的值和其他内部值,例如 'adptPolVal'、'eEcoId' 和 Source 结构。

我是 java 的新手,我正在尝试但失败了。 任何帮助将不胜感激! 提前致谢!

这是我的 json 文件内容...

{
  "consumer": {
    "name": "Consumer1"
  },
  "provider": {
    "name": "provider_a"
  },
  "interactions": [
    {
      "description": "API Score",
      "providerStates": [
        {
          "name": "",
          "params": {}
        }
      ],
      "request": {
        "method": "post",
        "path": "Z123MI6/services/score",
        "headers": {
          "content-type": "application/json"
        },
        "body": {
          "adptPolVal": true,
          "datapoints": [
            {
              "dataId": " data.point.id ",
              "source": {
                "srcType": "sourceType.dev.admin",
                "snum": "12345",
                "instId": "intance id",
                "contId": "container id",
                "appId": "com.consumer."
              },
              "userId": "userId",
              "ts": 1234567891011,
              "lt": 11.12345,
              "lng": 123.456,
              "ipId": "192.168.1.1",
              "geoGraph": ""
            }
          ],
          "eEcoId": "ecoId"
        }
      },
      "response": {
        "status": 200
      }
    }
  ]
}  

【问题讨论】:

  • 我的问题是,如果元素在数组数组中,你如何捕获它。这不是上述链接的重复。

标签: java json parsing pact jsonparser


【解决方案1】:

尝试替换此行

JSONArray req= (JSONArray) jsonObject.get("interactions");

与:

JSONArray req= (JSONArray)jsonObject.getJSONArray("interactions");

【讨论】:

  • 没有帮助.... :(
【解决方案2】:

您可以使用以下类作为参考来获得您想要的价值。

请务必使用 getJSONArray() 和 getJSONObject() 方法 根据您的输出进行适当的调整。

package com.test.test;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

public class Test {

    public static void main(String[] args) throws IOException, JSONException {
        String filePath = "C://Users//hello//Desktop//New Text Document.txt";
        String string = FileUtils.readFileToString(new File(filePath));
        JSONObject jsonObject = new JSONObject(string);
        JSONArray jsonArray = jsonObject.getJSONArray("interactions");
         for (int i = 0; i < jsonArray.length(); i++) {
             JSONObject reqObj = jsonArray.getJSONObject(i);
             System.out.println("description: " + reqObj.get("description") +"\n");
             System.out.println("request body: " + reqObj.get("request")); // prints full request body 
             System.out.println("reponse body: " + reqObj.get("response") +"\n"); // Success
             JSONObject requestBoday = reqObj.getJSONObject("request");
             JSONObject body = requestBoday.getJSONObject("body");

         }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-16
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    相关资源
    最近更新 更多