简体   繁体   中英

How to convert JSON Object to JSON Array and map it to custom POJO?

I have response like this

{
    "RCDSO - Production Environment Cost": {
        "href": "href1"
    },
    "RCDSO - Development & UAT Environment Cost": {
        "href": "href2"
    },
    "RCDSO - Total Cost for Prod - Compugen Managed": {
        "href": "href3"
    },
    "RCDSO - Virtual Machine Cost": {
        "href": "href4"
    },
    "RCDSO - Azure File Storage Cost": {
        "href": "href5"
    },
    "RCDSO - Azure Backup and Site Recovery": {
        "href": "href6"
    },
    "RCDSO - Azure App Services Cost": {
        "href": "href7"
    }
}

I want to map above JSON into ReportResponse.java POJO class.

public class ReportResponse {
    private String name;
    private String href;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getHref() {
        return href;
    }
    public void setHref(String href) {
        this.href = href;
    }
}

So that when I return JSON through my Rest API, response should look like as below:

    {
      "response":[
        {
            "name" : "RCDSO - Production Environment Cost",
            "href" : "href1"
        },
        {
            "name" : "RCDSO - Development & UAT Environment Cost",
            "href" : "href2"
        },
        {
            "name" : "RCDSO - Total Cost for Prod - Compugen Managed",
            "href" : "href3"
        }
        ..... so on....
    ]
}

I have tried to get response from external API and extracted json object from it and then tried it to convert to json array.

public String getReportList(String clientApiId) {
    String response = null;
    ResponseEntity<String> responseEntity = null;
    try {
        final String url = "https://chapi.cloudhealthtech.com/olap_reports/custom?client_api_id="+clientApiId;
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders header = new HttpHeaders();
        header.add(HttpHeaders.AUTHORIZATION, "Bearer "+apiKey);
        header.add(HttpHeaders.ACCEPT, "application/json");
        
        HttpEntity<String> requestEntity = new HttpEntity<String>("body",header);
        responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
        
        response = responseEntity.getBody();
        
        JSONObject obj = new JSONObject(response);
        JSONObject linkJsonObj = obj.getJSONObject("links");
        
        Iterator itr = linkJsonObj.keys();
        JSONArray array = new JSONArray();
        while(itr.hasNext()) {
            String key = (String)itr.next();
            array.put(linkJsonObj.get(key));
        }
        System.out.println("Json Array : "+array);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
    return response;
}

Please help me to achieve expected JSON and to map to ReportResponse.java POJO class.

You can check with: https://json2csharp.com/code-converters/json-to-pojo to map using ObjectMapper

if your 7 rows will be always the same or just create ReportResponse with List of single entry (pojo) and map to obj eg ReportEntry (name, href) then just return this list as response

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