0

我想遍历 JsonNode 对象的所有节点,并希望从 JsonNode 对象下方获取价格详细信息,有人可以帮助我如何获取低于价格的详细信息

回复

{
  "content": {
    "cart": {
      "logs": {
        "logs_cart_100": {
          "method": "over",
          "items": {
            "111": {
              "firstName": "Ram",
              "lastName": "K",
              "price": {
                "amount": 100.0,
                "baseAmount": 100.0,
                
              }
            },
            "222": {
              "firstName": "Velvan",
              "lastName": "S",
              "price": {
                "amount": 200.0,
                "baseAmount": 200.0,
                
              }
            },
            "333": {
              "firstName": "Dinesh",
              "lastName": "M",
              "price": {
                "amount": 300.0,
                "baseAmount": 300.0,
                
              }
            }
          }
        }
      },
      "promotions": [
        {
          "promotionName": "Dummy-1"
        },
        {
          "promotionName": "Dummy-2"
        },
        {
          "promotionName": "Dummy-3"
        }
      ]
    }
  }
}


ObjectMapper mapper = new ObjectMapper();
            JsonNode actualObj = mapper.readValue(response, JsonNode.class);
4

1 回答 1

0

首先,我会创建PriceItem类。然后您可以使用readTree(String). 最后,您可以stream将节点收集到一个Map<String, Item>.

价格.java:

public class Price {

    final double amount;

    final double baseAmount;

    public Price(double amount, double baseAmount) {
        this.amount = amount;
        this.baseAmount = baseAmount;
    }

    public static Price ofJsonNode(JsonNode node) {
        double amount = node.get("amount").asDouble();
        double baseAmount = node.get("baseAmount").asDouble();
        return new Price(amount, baseAmount);
    }

    public double getAmount() {
        return amount;
    }

    public double getBaseAmount() {
        return baseAmount;
    }

    @Override
    public String toString() {
        return "[amount=" + amount + ", baseAmount=" + baseAmount + "]";
    }

}

项目.java:

public class Item {

    private final String firstName;

    private final String lastName;

    private final Price price;

    public Item(String firstName, String lastName, Price price) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.price = price;
    }

    public static Item ofJsonNode(JsonNode node) {
        String firstName = node.get("firstName").asText();
        String lastName = node.get("lastName").asText();
        Price price = Price.ofJsonNode(node.get("price"));
        return new Item(firstName, lastName, price);
    }

    public final String getFirstName() {
        return firstName;
    }

    public final String getLastName() {
        return lastName;
    }

    public final Price getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return "[firstName=" + firstName + ", lastName=" + lastName + ", price=" + price + "]";
    }

}

然后你可以这样做:

ObjectMapper mapper = new ObjectMapper();

JsonNode root = mapper.readTree(response);

JsonNode itemsNode = root.get("content")
                         .get("cart")
                         .get("logs")
                         .get("logs_cart_100")
                         .get("items");

Iterator<Map.Entry<String, JsonNode>> nodesIterator = itemsNode.fields();
    
Stream<Map.Entry<String, JsonNode>> nodesStream = StreamSupport.stream(
        Spliterators.spliteratorUnknownSize(nodesIterator, 0), false);
    
Map<String, Item> items = nodesStream.collect(
        Collectors.toMap(Map.Entry::getKey, node -> Item.ofJsonNode(node.getValue())));

items.forEach((id, item) -> System.out.println(id + ": " + item));

输出:

111: [firstName=Ram, lastName=K, price=[amount=100.0, baseAmount=100.0]]
222: [firstName=Velvan, lastName=S, price=[amount=200.0, baseAmount=200.0]]
333: [firstName=Dinesh, lastName=M, price=[amount=300.0, baseAmount=300.0]]
于 2021-10-20T18:20:49.807 回答