I am calling another api and getting the following json response
{ "metadata": {}, "data": { "productid": 102001, "productname": "p101", "branddetail": { "brandid": 3840, "brandname": "abc", "brandcode": "x01" } } }
How to unwrap the brand details and read it as a class entity like below?
HttpGet httpGet = buildHttpGet("/externalApiURL"); HttpResponse response = getHttpClient().execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null && response.getStatusLine().getStatusCode() == HttpStatus.OK.value()) { ObjectMapper objectMapper = new ObjectMapper(); BrandDetail brandDetail = objectMapper.readValue(entity.getContent(), BrandDetail.class); }
Thanks in advance
Correct answer
Use convertvalue()
, here is a test.
@data public class branddetail { private int brandid; private string brandname; private string brandcode; }
@Test public void demo() throws Exception { ObjectMapper mapper = new ObjectMapper(); var data = """ { "metadata": {}, "data": { "productId": 102001, "productName": "P101", "brandDetail": { "brandId": 3840, "brandName": "ABC", "brandCode": "X01" } } } """; JsonNode node = mapper.readTree(data); JsonNode brandNode = node.get("data").get("brandDetail"); BrandDetail brandDetail = mapper.convertValue(brandNode, BrandDetail.class); // BrandDetail(brandId=3840, brandName=ABC, brandCode=X01) System.out.println(brandDetail); }
The above is the detailed content of How to unpack nested json objects into entities. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version
God-level code editing software (SublimeText3)