第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法訪問json文件內容

無法訪問json文件內容

守著一只汪 2022-06-04 16:10:46
我正在使用 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響應正文:{“狀態(tài)”:200}我正在努力訪問請求正文內容。及其子部分。我想訪問 'path' 的值和其他內部值,如 'adptPolVal'、'eEcoId' 和 Source 結構。我是java的新手,我嘗試但失敗了。任何幫助將不勝感激!提前致謝 !這是我的json文件內容...{  "consumer": {    "name": "Consumer1"  },  "provider": {    "name": "provider_a"  },  "interactions": [    {      "description": "API Score",      "providerStates": [        {          "name": "",          "params": {}        }      ],
查看完整描述

2 回答

?
鴻蒙傳說

TA貢獻1865條經(jīng)驗 獲得超7個贊

您可以使用以下類作為參考來獲得您想要的值。


請確保根據(jù)您的輸出正確使用 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");


         }

    }

}


查看完整回答
反對 回復 2022-06-04
?
qq_笑_17

TA貢獻1818條經(jīng)驗 獲得超7個贊

嘗試替換此行

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

和 :

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


查看完整回答
反對 回復 2022-06-04
  • 2 回答
  • 0 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號