簡體   English   中英

如何格式化 JSON 響應並將它們作為文件存儲在目錄結構中?

[英]How to format the JSON responses and store them as files in a directory structure?

我正在開發一個 Springboot 應用程序,它使用 RESTCONF 從網絡獲取數據。 我在 application.properties 中保存了帶有前綴(“restconf”)的 url,並在運行時獲取它們。 我想將每個 url 的 JSON 字符串響應保存在一個單獨的 JSON 文件中,其路徑將在 application.properties 中聲明。 這是我的 application.properties

restconf.restconfUrl = ${restconf.IP}/restconf
restconf.configUrl = ${restconf.restconfUrl}/config
restconf.operationalUrl = ${restconf.restconfUrl}/operational

restconf.networkTopologyURI = /network-topology:network-topology
restconf.networkTopologyId =  /topology/{topology-id}
restconf.networkTopologyTypes = /topology-types

restconf.networkTopologyNode = /node/{node-id}

restconf.networkTopologyLink = /link/{link-id}
restconf.networkTopologyTp =  /termination-point/{tp-id}

我正在訪問此回復

LinkedMultiValueMap<String,String> headers = new LinkedMultiValueMap<>();

        // Get Config Topology
        String response = restService.Service("",headers, HttpMethod.GET,String.class,restconfProperties.getTopologyConfigUrl(), Collections.EMPTY_MAP);
        System.out.print(response);

使用 nlohmann 庫將 JSON 存儲到文件中。 這很容易並且有據可查。 例如,它看起來像這樣:

#include "json.hpp"
#include <fstream>
using jsonf = nlohmann::json;

int main() {
    jsonf jsonfile;

    jsonfile["foo"] = "bar";

    std::ofstream file("key.json");
    file << jsonfile;
}

您可以使用這種方法存儲所有條目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM