Created
March 24, 2020 22:42
-
-
Save arsenvlad/126e5d27a37ff2c23b30dcae239bc246 to your computer and use it in GitHub Desktop.
Java Jackson JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package arsenvlad; | |
import java.util.Calendar; | |
public class JacksonExample { | |
private String fieldOne; | |
private String fieldTwo; | |
private Calendar someDate; | |
public String getFieldOne() { | |
return fieldOne; | |
} | |
public void setFieldOne(String fieldOne) { | |
this.fieldOne = fieldOne; | |
} | |
public String getFieldTwo() { | |
return fieldTwo; | |
} | |
public void setFieldTwo(String fieldTwo) { | |
this.fieldTwo = fieldTwo; | |
} | |
public Calendar getSomeDate() { | |
return someDate; | |
} | |
public void setSomeDate(Calendar someDate) { | |
this.someDate = someDate; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
try { | |
List<arsenvlad.JacksonExample> list = new ArrayList<arsenvlad.JacksonExample>(); | |
for (int i=0; i<3; i++) { | |
arsenvlad.JacksonExample o = new arsenvlad.JacksonExample(); | |
o.setFieldOne("fieldOneValue"); | |
o.setFieldTwo("fieldTwoValue"); | |
o.setSomeDate(Calendar.getInstance()); | |
list.add(o); | |
} | |
ObjectMapper mapper = new ObjectMapper(); | |
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm a z"); | |
mapper.setDateFormat(df); | |
mapper.enable(SerializationFeature.INDENT_OUTPUT); | |
String json = mapper.writeValueAsString(list); | |
System.out.println(json); | |
} catch (JsonProcessingException ex) { } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ { | |
"fieldOne" : "fieldOneValue", | |
"fieldTwo" : "fieldTwoValue", | |
"someDate" : "2020-03-24 17:42 PM CDT" | |
}, { | |
"fieldOne" : "fieldOneValue", | |
"fieldTwo" : "fieldTwoValue", | |
"someDate" : "2020-03-24 17:42 PM CDT" | |
}, { | |
"fieldOne" : "fieldOneValue", | |
"fieldTwo" : "fieldTwoValue", | |
"someDate" : "2020-03-24 17:42 PM CDT" | |
} ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment