Last active
October 19, 2018 13:40
-
-
Save Cvetomird91/13042c0fc3ff4fb21d82d30b8a166455 to your computer and use it in GitHub Desktop.
Map JSON data to payment objects and filter them.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.fasterxml.jackson.databind.ObjectMapper; | |
| import java.io.File; | |
| import java.io.IOException; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.Arrays; | |
| public class JacksonExamples { | |
| public static void main(String[] args) throws IOException { | |
| //read the JSON file into POJOs | |
| ObjectMapper objectMapper = new ObjectMapper(); | |
| File file = new File("./data.json"); | |
| Payment[] payments = objectMapper.readValue(file, Payment[].class); | |
| //convert the property representing the transactions into an ArrayList | |
| List<Payment> paymentsList = new ArrayList<Payment>(Arrays.asList(payments)); | |
| //Filter the transactions whose status isn't received | |
| paymentsList.removeIf((Payment p) -> p.getStatus() == null || !p.getStatus().equals("Received")); | |
| for (Payment p : paymentsList) { | |
| System.out.println(p.getStatus()); | |
| } | |
| } | |
| } | |
| class Payment { | |
| private int paymentID; | |
| private int paymentProfileID; | |
| private String created; | |
| private String status; | |
| private String value1; | |
| private String transactionID; | |
| public int getPaymentID() { | |
| return paymentID; | |
| } | |
| public void setPaymentID(int paymentID) { | |
| this.paymentID = paymentID; | |
| } | |
| public String getCreated() { | |
| return created; | |
| } | |
| public void setCreated(String created) { | |
| this.created = created; | |
| } | |
| public String getStatus() { | |
| return status; | |
| } | |
| public void setStatus(String status) { | |
| this.status = status; | |
| } | |
| public String getValue1() { | |
| return value1; | |
| } | |
| public void setValue1(String value1) { | |
| this.value1 = value1; | |
| } | |
| public String getTransactionID() { | |
| return transactionID; | |
| } | |
| public void setTransactionID(String transactionID) { | |
| this.transactionID = transactionID; | |
| } | |
| public int getPaymentProfileID() { | |
| return paymentProfileID; | |
| } | |
| public void setPaymentProfileID(int paymentProfileID) { | |
| this.paymentProfileID = paymentProfileID; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment