Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

Create a POJO (GSON--toJson)

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
SiemensSEN
Asked:
SiemensSEN
1 Solution
 
Amitkumar PanchalSr. ConsultantCommented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now