Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get a value list of a enum to a json object

I want to create a json object with values of "3D Tour", "Videos", "Photos Only", etc. You can find the enum class below. How can I implement that?

package com.padgea.listing.application.dto;

public enum General implements Catalogue {

Tour("3D Tour"),
Videos("Videos"),
Photos_Only("Photos Only"),
Price_Reduced("Price Reduced"),
Furnished("Furnished"),
Luxury("Luxury");

private final String value;

General(String value) {
    this.value = value;
}

public String getValue() {
    return value;
}

}

I need a output like this
{General : "3D Tour","Videos","Photos Only",etc}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This will return a list of strings containing all the values.

    enum General implements Catalogue {

    Tour("3D Tour"),
    Videos("Videos"),
    Photos_Only("Photos Only"),
    Price_Reduced("Price Reduced"),
    Furnished("Furnished"),
    Luxury("Luxury");

    private final String value;

    General(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }
    
    public static List<String> valuesList() {
        return Arrays.stream(General.values())
                          .map(General::getValue)
                          .collect(Collectors.toList());
    }
}

And a level up you’ll do something like

myJson.put("General", General.valuesList())

An output will be

 {
    "General": ["3D Tour","Videos","Photos Only"]
 }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading