JSON (Employee details):
Java Sample code:
Sample output:
{ "payload": { "audit": { "enteredBy": "NIKHIL", "entryType": "ORDER" }, "employees": { "employee": [ { "lineNo": 1, "name": "NIKHIL RANJAN", "email": "NIKHILRANJAN@DEVINLINE.COM", "address": { "firstLine":"WINDSOR APT", "SecondLine":"#204, BLOCK 2", "state":"KARTA", "postalCode":"50081", "phone": { "home": "4747521304", "work": "9748767304" } }, "org": "IT", "department": "DPC" } ], "requestType": "EMPLOYEE_DETAILS" } } }
Java Sample code:
import java.io.IOException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonNode; public class JacksonJsonNodeParser { private static com.fasterxml.jackson.databind.ObjectMapper getJacksonMapper() { com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); return mapper; } private static void doJsonTreeParse(String employeeJSON) throws IOException { JsonNode responseJsonNode = getJacksonMapper().readTree(employeeJSON); JsonNode employeesDetails = responseJsonNode.get("payload").get("employeesDetails"); JsonNode employees = employeesDetails.get("employees"); String status = employeesDetails.get("requestType").asText(); System.out.println(status); if (employees.isArray()) { for (final JsonNode employee : employees) { String name = employee.get("name").asText(); String email = employee.get("email").asText(); JsonNode address = employee.get("address"); JsonNode phone = address.get("phone"); String homePhone = phone.get("home").asText(); System.out.println("*** Employee Detail ***"); System.out.println("Name: " + name); System.out.println("Email: " + email); System.out.println("HomePhone: " + homePhone); } } } public static void main(String[] args) throws JsonProcessingException, IOException { String employeeJSON = "{\n" + " \"payload\": {\n" + " \"audit\": {\n" + " \"enteredBy\": \"NIKHIL\",\n" + " \"entryType\": \"ORDER\"\n" + " },\n" + " \"employeesDetails\": {\n" + " \"employees\": [\n" + " {\n" + " \"lineNo\": 1,\n" + " \"name\": \"NIKHIL RANJAN\",\n" + " \"email\": \"NIKHILRANJAN@DEVINLINE.COM\",\n" + " \"address\": {\n" + " \"firstLine\":\"WINDSOR APT\",\n" + " \"SecondLine\":\"#204, BLOCK 2\",\n" + " \"state\":\"KARTA\",\n" + " \"postalCode\":\"50081\",\n" + " \"phone\": {\n" + " \"home\": \"4747521304\",\n" + " \"work\": \"9748767304\"\n" + " }\n" + " },\n" + " \"org\": \"IT\",\n" + " \"department\": \"DPC\"\n" + " }\n" + " ],\n" + " \"requestType\": \"EMPLOYEE_DETAILS\"\n" + " }\n" + " }\n" + "}"; doJsonTreeParse(employeeJSON); } }
Sample output:
EMPLOYEE_DETAILS
*** Employee Detail ***
Name: NIKHIL RANJAN
Email: NIKHILRANJAN@DEVINLINE.COM
HomePhone: 4747521304
Hey, thank you for sharing this. Informatica Read Json
ReplyDeleteĐặt vé máy bay tại Aivivu, tham khảo
ReplyDeletegiá vé máy bay đi Mỹ khứ hồi
thời gian bay từ los angeles về việt nam
giá vé máy bay từ Toronto đến việt nam
khi nào mở lại đường bay nhật bản việt nam
vé máy bay từ hàn quốc về việt nam
Vé máy bay từ Đài Loan về Việt Nam
khách sạn cách ly ở việt nam
ve may bay chuyen gia nuoc ngoai sang Viet Nam