Table of Contents
Correct answer
Home Java How to unpack nested json objects into entities

How to unpack nested json objects into entities

Feb 06, 2024 am 09:48 AM

Question content

I am calling another api and getting the following json response

1

2

3

4

5

6

7

8

9

10

11

12

13

{

    "metadata": {},

    "data": {

        "productid": 102001,

        "productname": "p101",

 

        "branddetail": {

            "brandid": 3840,

            "brandname": "abc",

            "brandcode": "x01"

        }

    }

}

Copy after login

How to unwrap the brand details and read it as a class entity like below?

1

2

3

4

5

6

7

8

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

    }

Copy after login

Thanks in advance


Correct answer


Use convertvalue(), here is a test.

1

2

3

4

5

6

@data

public class branddetail {

    private int brandid;

    private string brandname;

    private string brandcode;

}

Copy after login

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

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

}

Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)