How can use reflection to get the property names and values from a POJO?

后端 未结 2 1563
小蘑菇
小蘑菇 2021-01-18 03:51

So I\'m writing an \"POJO to JSON\" converter. I want to be able to pass in a List object and convert to JSON.

Hopefully this will make sense<

2条回答
  •  悲哀的现实
    2021-01-18 04:12

    You need to use #getDeclaredFields() to include private fields, #getFields() only lists the public ones.

    With private fields you will also run into access restriction problems, so you probably want to look into the Field#setAccessible() method as well.

提交回复
热议问题