gpt4 book ai didi

java - 尝试从 JSON 中嵌套的数组读取值

转载 作者:行者123 更新时间:2023-12-02 13:28:20 25 4
gpt4 key购买 nike

我一直在使用 Jackson 编写一个 Rest 服务来提取响应值中的名称和 sizeFromStorage。

我创建了以下类来尝试执行此操作:

import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(value = {"status", "header"})
public class Vaults {

private List<Object> response;

public Vaults(){

}

public Vaults(List<Object> response){
this.response = response;
}

public List<Object> getResponse(){
return response;
}

public void setResponse(List<Object> response) {
this.response = response;
}

@Override
public String toString() {
return "" + response;
}
}


import java.io.IOException;
import java.util.List;
import java.util.ArrayList;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import java.io.InputStream;

public class JacksonObjectModelTest {

public static void main(String[] args) throws IOException {
String jsonFileName = "/JsonRead/json.json";
List<Vaults> emps = new JacksonObjectModelTest().getVaultList(jsonFileName);
System.out.println(emps.toString());
}

public ArrayList<Vaults> getVaultList(String jsonFileName) throws IOException {
//read json file data to String
InputStream inputStream = getClass().getResourceAsStream(jsonFileName);

//create ObjectMapper instance
ObjectMapper objectMapper = new ObjectMapper().enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);

//convert json string to object
CollectionType collectionType = objectMapper.getTypeFactory().constructCollectionType(List.class, Vaults.class);
ArrayList<Vaults> emps = objectMapper.readValue(inputStream, collectionType);
return (ArrayList<Vaults>) emps;
}
}

{
"status": "ok",
"header": {
"now": 1491545894581,
"status": "ok",
"requestId": "WOcvJsCoAmoAAESbkBYAAAB5"
},
"response": {
"vault": [
{
"id": 20,
"name": "Apple",
"description": "",
"sizeFromStorage": 95957225298,
"storagePools": [
{
"storagePool": {
"id": 1,
"name": "storage-pool1",
"sizeFromStorage": 95957225298,
"generations": [
{
"generation": {
"sequence": 0
}
}
]
}
}
]
},
{
"id": 21,
"name": "Banana",
"description": "",
"sizeFromStorage": 98957268244,
"storagePools": [
{
"storagePool": {
"id": 2,
"name": "storage-pool1",
"sizeFromStorage": 98957268244,
"generations": [
{
"generation": {
"sequence": 0
}
}
]
}
}
]
},
]
}
}

我从中得到的输出是:

[[{vaults=[{id=20, name=Apple, description=, sizeFromStorage=95957225298, storagePools=[{storagePool={id=1, name=storage-pool1, sizeFromStorage=5043, estimateUsableTotalLogicalSizeFromStorage=95957225298, generations=[{generation={sequence=0}}]}}]}, {id=20, name=Apple, description=, sizeFromStorage=95957225298, storagePools=[{storagePool={id=1, name=storage-pool1, sizeFromStorage=5043, estimateUsableTotalLogicalSizeFromStorage=95957225298, generations=[{generation={sequence=0}}]}}]}]]

但是,我想要做的是 name 和 sizeFromStorage 值。到目前为止,我已经成功地去掉了前三个值。不幸的是,我现在陷入困境,因为我不太熟悉 Rest 服务或读取 JSON。有什么方法可以进一步深入研究 JSON 以获得我需要的东西,或者我是否以错误的方式处理这个问题?

附加信息:自从发布我最初的问题以来,我想到了这个(基于我在不同网站上看到的内容):

package JsonRead;

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

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonTreeModel {
public static void main(String[] args) {

try{

ObjectMapper mapper = new ObjectMapper();

JsonNode root = mapper.readTree(new File("JsonRead/json.json"));

JsonNode vaultsNode = root.path("response").path("vault");

/*if(vaultsNode.isArray()){
for(JsonNode node : vaultsNode){
String name = node.path("name").asText();
System.out.println("Array Name: " + name);
}
}*/

for(JsonNode node : vaultsNode){
String name = node.path("name").asText();
String bytesUsed = node.path("sizeFromStorage").asText();
System.out.println("Name: " + name + ", Bytes Used: " + bytesUsed);
}

} catch(IOException e){
System.out.println("IO Exception " + e );
}
}
}

它可以满足我的需求,但是有更好的方法吗?

威尔,谢谢您的回复。我会研究你的建议。

最佳答案

好的,所以我刚刚重新阅读了您的问题。

建议您自己创建一个具有更有意义的自定义对象类型的 VaultDAO 对象,以用于响应集合。如果您使用相同的变量名称(看起来您知道),那么它应该为您反序列化

关于java - 尝试从 JSON 中嵌套的数组读取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43294069/

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