Solved

Create a POJO (GSON--toJson)

Posted on 2014-07-21
1
240 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 20

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now