无法访问json文件内容

我正在使用 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

           }

        }

它的输出:


++++++++++++++++++++ {"full json file content prints"}

提供者名称:SIS 描述:API POST Score 请求正文:{"full request body prints"} 路径:null响应正文:{“状态”:200}


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


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


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


{

  "consumer": {

    "name": "Consumer1"

  },

  "provider": {

    "name": "provider_a"

  },

  "interactions": [

    {

      "description": "API Score",

      "providerStates": [

        {

          "name": "",

          "params": {}

        }

      ],

守着一只汪
浏览 125回答 2
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 {&nbsp; &nbsp; public static void main(String[] args) throws IOException, JSONException {&nbsp; &nbsp; &nbsp; &nbsp; String filePath = "C://Users//hello//Desktop//New Text Document.txt";&nbsp; &nbsp; &nbsp; &nbsp; String string = FileUtils.readFileToString(new File(filePath));&nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObject = new JSONObject(string);&nbsp; &nbsp; &nbsp; &nbsp; JSONArray jsonArray = jsonObject.getJSONArray("interactions");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (int i = 0; i < jsonArray.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONObject reqObj = jsonArray.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("description: " + reqObj.get("description") +"\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("request body: " + reqObj.get("request")); // prints full request body&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("reponse body: " + reqObj.get("response") +"\n"); // Success&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONObject requestBoday = reqObj.getJSONObject("request");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONObject body = requestBoday.getJSONObject("body");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }}

qq_笑_17

尝试替换此行JSONArray&nbsp;req=&nbsp;(JSONArray)&nbsp;jsonObject.get("interactions");和 :JSONArray&nbsp;req=&nbsp;(JSONArray)jsonObject.getJSONArray("interactions");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java