Solved

Create a POJO (GSON--toJson)

Posted on 2014-07-21
1
243 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
1 Comment
 
LVL 21

Accepted Solution

by:
Amitkumar Panchal earned 250 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
thymeleaf natural templating vs JSP 2 95
jar file executable 12 51
Eclipse with gitlab 1 37
null output 3 24
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

806 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