gpt4 book ai didi

java - 当我获取字符串格式的 json 内容时,如何获取 json 键?

转载 作者:行者123 更新时间:2023-12-01 09:37:46 25 4
gpt4 key购买 nike

我从 URI 获取字符串格式的 json。

String srvdiagJson = getJsonFromHttpConnection(diagcon);

我的 json 数据如下所示。

{"hdd_errors":18446744073709552000,"vnet2_tx_packets":86647,"vnet2_rx":19753895921,"vda_write":2873672704,"hdd_read":180702,"vnet2_tx":15722682,"vnet2_rx_drop":691,"vnet2_rx_errors":0,"memory-swap_in":0,"hdd_read_req":78,"vnet2_tx_drop":0,"vnet2_tx_errors":0,"hdd_write":0,"memory":2097152,"memory-rss":1598552,"cpu0_time":48668260000000,"vda_read":251817472,"vda_write_req":267405,"vnet2_rx_packets":225351039,"memory-actual":2097152,"hdd_write_req":0,"vda_read_req":16222,"vda_errors":18446744073709552000}

我认为不需要解析数据。但我也尝试根据建议解析它并将其保存在对象和数组中。但我无法访问任何值。

最佳答案

您可以使用 jackson 将 json 字符串转换为 java 对象,反之亦然。例如

ObjectMapper mapper = new ObjectMapper();
String jsonStr = "{"hdd_errors":18446744073709552000,"vnet2_tx_packets":86647,"vnet2_rx":19753895921,"vda_write":2873672704,"hdd_read":180702,"vnet2_tx":15722682,"vnet2_rx_drop":691,"vnet2_rx_errors":0,"memory-swap_in":0,"hdd_read_req":78,"vnet2_tx_drop":0,"vnet2_tx_errors":0,"hdd_write":0,"memory":2097152,"memory-rss":1598552,"cpu0_time":48668260000000,"vda_read":251817472,"vda_write_req":267405,"vnet2_rx_packets":225351039,"memory-actual":2097152,"hdd_write_req":0,"vda_read_req":16222,"vda_errors":18446744073709552000}";

//JSON from String to Object
HddError obj = mapper.readValue(jsonStr, HddError.class);

关于java - 当我获取字符串格式的 json 内容时,如何获取 json 键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38713301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com