Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Create an array of objects when marshalled to json
Create an array of objects when marshalled to json [message #1840345] Mon, 12 April 2021 23:09 Go to next message
Brian Lavender is currently offline Brian LavenderFriend
Messages: 20
Registered: July 2009
Junior Member
I have eclipselink working so that it can marshall an object out to JSON, yet I would like to marshall out an array of ojbects. How do I do that? See details below. I tried to be concise, so please forgive me if I made a coding typo.

Brian

Customer customer = new Customer();
customer.setCompanyName("Fancy Diddly");
// other setters

Marshaller marshaller;
marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
marshaller.setProperty(MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
//marshaller.setProperty(MarshallerProperties.JSON_, true);

marshaller.marshal(customer, System.out);


{
   "registered_state_abbr" : "CA",
   "state_registration_number" : "789",
   "registration_status" : "Active",
   "operation_type_id" : 1,
   "fed_regulated" : "Yes",
   "ein" : "12-34",
   "company_name" : "Fancy Diddly",
   "phone_no" : "916-123-5432"
}


Yet, I would like to have an array of customers and marshall theme out to an array.

List<Customer> customers = new ArrayList<Customer>();
customers.add(cust1);
customers.add(cust2);
// add customers to the list 


And then the output would be as follows in JSON.

[
	{
	   "registered_state_abbr" : "CA",
	   "state_registration_number" : "789",
	   "registration_status" : "Active",
	   "operation_type_id" : 1,
	   "fed_regulated" : "Yes",
	   "ein" : "12-34",
	   "company_name" : "Fancy Diddly",
	   "phone_no" : "916-123-5432"
	},
	{
	   "registered_state_abbr" : "CA",
	   "state_registration_number" : "1337",
	   "registration_status" : "Active",
	   "operation_type_id" : 1,
	   "fed_regulated" : "Yes",
	   "ein" : "56-89",
	   "company_name" : "Bo Schmooh",
	   "phone_no" : "916-456-9876"
	}
]
Re: Create an array of objects when marshalled to json [message #1840346 is a reply to message #1840345] Mon, 12 April 2021 23:18 Go to previous message
Brian Lavender is currently offline Brian LavenderFriend
Messages: 20
Registered: July 2009
Junior Member
Oh goodness! It seems that in order to solve the problem, I must articulate it. I didn't realize that the exact syntax I posted would do just that.

Customer customer = new Customer();
customer.setCompanyName("Fancy Diddly");
// other setters

List<AMCRegistration> customers = new ArrayList<>();
customers.add(customer);

Marshaller marshaller;
marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
marshaller.setProperty(MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
//marshaller.setProperty(MarshallerProperties.JSON_, true);

marshaller.marshal(customers, System.out);
Previous Topic:Unrecoverable OptimisticLockException
Next Topic:Trying to send array of objects on json
Goto Forum:
  


Current Time: Wed Apr 24 22:02:22 GMT 2024

Powered by FUDForum. Page generated in 0.02858 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top