6

Good day all, I would like to remove the property name when Serializing a Pojo to json. I have the following class

public class Field {

    private List<SubFieldItems> subFieldItems;

    public List<SubFieldItems> getSubFieldItems() {
        return subFieldItems;
    }

    public void setSubFieldItems(List<SubFieldItems> subFieldItems) {
        this.subFieldItems = subFieldItems;
    }
   }

and the SubFieldItems class:

        public class SubFieldItems {
    @JsonPropertyOrder

        private String name;
        private List<String> items;


        public SubFieldItems(String name, List<String> items) {
            this.name = name;
            this.items = items;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<String> getItems() {
            return items;
        }

        public void setItems(List<String> items) {
            this.items = items;
        }

//tried doing this
      /*  @JsonValue
        @Override
        public String toString() {
            return "{" +
                    " \"name\":" + "\"" + name + "\"" +
                    ",\" items\":" + items +
                    '}';
        }*/
    }

when i serialize, the json Output is

{
  "field": {
    "subFieldItems": [
      {
        "name": "brands",
        " items": []
      }
    ]
  }
}

but i want to have the subFieldItems values without the property name like this:

   {
      "field": {
            [
          {
            "name": "brands",
            " items": []
          }
        ]
      }
    }

What Jackson annotation can i use, i have tried using @JsonValue as in the toString method to force that like in the comments, but i keep getting weird results. Please Any help is appreciated. Thanks

irobotxx
  • 5,963
  • 11
  • 62
  • 91
  • @FedericoPeraltaSchaffner maybe i did not word the question well, i would like to convert the Java Object to JSON. I get those values as i posted before but i want one without the "subFieldItems" in the json output, just its values – irobotxx Sep 15 '15 at 13:02

1 Answers1

12

The JSON you want is invalid, maybe this is what you want:

{
  "field": [
    {
      "name": "brands",
      "items": []
    }
  ]
}

(Note that I've removed the brace after "field":, as well as its matching closing brace).

In this case, you might find @JsonValue annotation useful:

@JsonValue: per-property marker to indicate that the POJO should serialization is to be done using value of the property, often a java.lang.String (like annotation toString() method).

Try this:

public class Field {

    private List<SubFieldItems> subFieldItems;

    @JsonValue
    public List<SubFieldItems> getSubFieldItems() {
        return subFieldItems;
    }

    public void setSubFieldItems(List<SubFieldItems> subFieldItems) {
        this.subFieldItems = subFieldItems;
    }
}
fps
  • 33,623
  • 8
  • 55
  • 110
  • 1
    you are right!.. just noticed the mistake now. Thanks for the help. – irobotxx Sep 15 '15 at 14:29
  • http://stackoverflow.com/questions/41306886/remove-the-automatically-display-of-getter-in-pojo This link contains similar problem. Can you check the same. I tried with JSON Value but no luck. – Souvik Dec 24 '16 at 14:20
  • 1
    Saved a lot of my time . Thank you so much – user_531 Jul 19 '18 at 19:07