简体   繁体   中英

How can we iterate all subNodes of a jsonNode object?

I want to iterate through all nodes of a JsonNode object, And want fetch price details from below JsonNode object , Can some one help me how can we fetch below price details

response

{
  "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);

First, I would create Price and Item classes. Then you can read the response as a tree using readTree(String) . Finally, you can stream the nodes and collect them to a Map<String, Item> .

Price.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 + "]";
    }

}

Item.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 + "]";
    }

}

Then you can do:

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));

Output:

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]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM