Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create a POJO (GSON--toJson)

Posted on 2014-07-21
1
Medium Priority
?
263 Views
Last Modified: 2014-08-04
Hello,
  I need to create  POJO that returns the following JSON string
{"Fields":{"summary":"summary","issueType":{"name":"task"},"project":{"key":"xyz"}})

Here is my class
public class JIssue 
{

	private String summary;
	private Map<String, String> issueType;
	private Map<String, String> project;
	public JIssue(Map<String, String> issueType,Map<String, String> project,String summary) 
	{
		// TODO Auto-generated constructor stub
		setSummary( summary);
		setIssueType( issueType);
		setProject( project);
		
	}
	public String getSumamry()
	{
		return summary;
	}
	public void setSummary(String summary)
	{
		this.summary= summary;
	}
	public Map<String, String> getIssueType() {
        return issueType;
    }
    public void setIssueType(Map<String, String> issueType) {
        this.issueType = issueType;
    }
    
    public Map<String, String> getProject() {
        return project;
    }
    public void setProject(Map<String, String> project) {
        this.project = project;
    }

}

Open in new window


MAIN code
public static void main(String[] args) 
	{
		Map<String, String> prj = new HashMap<String, String>();
		Map<String, String> issueType = new HashMap<String, String>();
		
                 prj.put("key", "xyz");
		issueType.put("name", "task");

		JIssue myIssue = new JIssue(issueType,prj,"summary");
		
                Gson gson = new Gson();
		String data = gson.toJson(myIssue);
		//System.out.println(data);
		
	}

Open in new window

the above code returns
{"summary":"summary","issueType":{"name":"task"},"project":{"key":"xyz"}}

I want
{"Fields":{"summary":"summary","issueType":{"name":"task"},"project":{"key":"xyz"}})

How can I change the class to return the above json string
0
Comment
Question by:SiemensSEN
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 21

Accepted Solution

by:
Amitkumar Panchal earned 1000 total points
ID: 40239510
You can create a class by wrapping all fields (ie. project, summary, issuetype) as given below:


import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;


public class JIssue 
{

	class JFields {

		private String summary;
		private Map<String, String> issueType;
		private Map<String, String> project;
		
		public JFields(Map<String, String> issueType, Map<String, String> project, String summary) {

			setSummary( summary);
			setIssueType( issueType);
			setProject( project);
		}

		private String getSumamry() {
			return summary;
		}
		
		private void setSummary(String summary) {
			this.summary= summary;
		}
		
		private Map<String, String> getIssueType() {
	        return issueType;
	    }
	    
		private void setIssueType(Map<String, String> issueType) {
	        this.issueType = issueType;
	    }
	    
	    private Map<String, String> getProject() {
	        return project;
	    }
	    
	    private void setProject(Map<String, String> project) {
	        this.project = project;
	    }
		
	}

	private JFields Fields; 
	
	
    public JFields getFields() {
    	return Fields;
    }
    
    public void setFields(JFields fields) {
        this.Fields = fields;
    }
	
	public JIssue(JFields fields) {

		setFields(fields);
	}
	
	public JIssue(Map<String, String> issueType, Map<String, String> project, String summary) {
		
		JFields fields = new JFields(issueType, project, summary);
		setFields(fields);
	}

    public static void main(String[] args) {
    	
		Map<String, String> prj = new HashMap<String, String>();
		Map<String, String> issueType = new HashMap<String, String>();
		
        prj.put("key", "xyz");
		issueType.put("name", "task");

		JIssue myIssue = new JIssue(issueType, prj, "summary");
		
        Gson gson = new Gson();
		String data = gson.toJson(myIssue);
		System.out.println(data);
	}
}

Open in new window


Output : {"Fields":{"summary":"summary","issueType":{"name":"task"},"project":{"key":"xyz"}}}
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question