简体   繁体   中英

converting java DTO list into JSON object

I have a DTO class like this:

package stbet.model.dto.db;

public class UKDashboardEventDTO implements Serializable{

private Long eventId;
private String meetingCode;
private String meetingName;
private String eventTime;
private String eventCode;
private String settleStatus;
private String category;

//getters and setters here:

@Override
public String toString() {
    return "eventList{" + "eventId=" + eventId + ", meetingCode=" + meetingCode + ", meetingName=" + meetingName
            + "eventTime=" + eventTime + ", eventCode=" + eventCode + ", settleStatus=" + settleStatus
            + ", category=" + category + '}';
}

}

and I do some query stuff and create a java List of above DTO type.

                   for(Event ev : eventList){
                        dto = new UKDashboardEventDTO();
                        // some stuff
                        dto.setEventCode(ev.getEventCode());
                        dto.setEventId(ev.getId());
                        dto.setEventTime(ev.getEventTime());
                        dto.setMeetingName(ev.getMeeting().getMeetingName());
                        dto.setMeetingCode(ev.getMeeting().getMeetingCode());

                        eventDTOList.add(dto);    
                    }

Then I add this list into a Hashmap and covert it into a JSON object like this:

Map map = new HashMap();
map.put("eventList",eventDTOList);

now convert into the json:

   JSONObject obj = new JSONObject();
        try {
            obj.put("eventMap", map);

        } catch (JSONException ex) {
        }
        out.println(obj);
        out.flush();
        out.close();

but when I get this object from client side, I am getting the dto package/object names list when parse or stringify the output instead of the proper dto values I passed from Java. What I get is this:

"{"eventMap":{"eventList":["stbet.model.dto.db.UKDashboardEventDTO@617538bb","stbet.model.dto.db.UKDashboardEventDTO@56dfaef9","stbet.model.dto.db.UKDashboardEventDTO@775889fd","stbet.model.dto.db.UKDashboardEventDTO@55cb7e41","stbet.model.dto.db.UKDashboardEventDTO@22ce0968","stbet.model.dto.db.UKDashboardEventDTO@4cb9cb2"]}}"

can you please let me know how to get the dto values I set from Java to client side json without java package name as above.

You should Override the toString field on the DTO, to print out all the individual field values. This toString will be called when you do out.println(obj);

eg.

toString() {
 // this method should list out all the attributes.
 }

You should be using GSONBuilder to create the gson. Examples: Gson: How to exclude specific fields from Serialization without annotations

Gson doesn't parse an exposed field

Here is a post on using GSON also

How to expose a method using GSon?

Firstly, you're using both JSON and GSON libraries, JSONException exists in JSON one and Expose annotation is in GSON. Please make sure you don't mix them as I won't work as intended.

Secondly, from Expose documentation

An annotation that indicates this member should be exposed for JSON serialization or deserialization.

This annotation has no effect unless you build Gson with a GsonBuilder and invoke GsonBuilder.excludeFieldsWithoutExposeAnnotation() method.

Finally I could fix it with your suggestions @Trynkiewicz Mariusz and @Paul John. What I did was: 1. remove @Expose annotation 2. overridden the toString(){...} method. 3. remove the map implementation and used a List. 4. used gson.toJson(eventList);

this solved the issue and the output now is like :

[{
"eventId":167804, "meetingCode":"V5PGB", "meetingName":"SprintValley", "eventTime":"15:38:00", "eventCode":"10:08:00", "category":"HR" }, {
"eventId":167805, "meetingCode":"V5PGB", "meetingName":"SprintValley", "eventTime":"15:50:00", "eventCode":"10:20:00", "category":"HR" },..]

Thanks again guys...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM