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
Solved

Create a POJO (GSON--toJson)

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web application structure 18 100
hibernate example issues from command prompt 10 50
How do I remove an object from a 3 40
What's wrong with this code? 4 26
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 Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

860 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