Solved

JSON manipulation in Java

Posted on 2013-05-13
2
289 Views
Last Modified: 2013-06-05
i have a Java class:

public class Task 
{
	private String _name, _description, _type;
	
	 public String getName() { return _name; }
     public void setName(String name) { _name = name; }
     
     public String getDescription() { return _description; }
     public void setDescription(String description) {  _description = description; }
     
     public String getType() { return _type; }
     public void setType(String type) { _type = type; }
}

Open in new window


i want to convert a list of these objects into this:

'aoColumns':[
                     { "sTitle": "Number" },
                           { "sTitle": "Description" },
                           { "sTitle": "Task Type" },
                   ],
                'aaData':[['Task1','This is task 1','Type1'],
                           ['Task2','This is task 2','Type2'],
                           ['Task3','This is task 3','Type3'],
                ]
               }

Open in new window


so the headers is the first array, which is static.  what follows is a list of lists representing the data (without the header/property name).

is there an API or library that can do that?  i have tried Jackson, but when it outputs the string, it comes out as key values:

[{"name":"Task1","type":"Type1","description":"'This is task 1'"},{"name":"Task2","type":"Type2","description":"'This is task 2"}]

thanks.
0
Comment
Question by:mmingfeilam
2 Comments
 
LVL 26

Accepted Solution

by:
dpearson earned 250 total points
ID: 39163541
You can use a JSON library like JSON-lib (http://json-lib.sourceforge.net/) to do this pretty easily yourself.

Something like this (doing this off the cuff so probably not exactly right):

JSONObject json = new JSONObject();

       JSONObject columns= new JSONObject();
        columns.put( "sfTitle", "Name");
        columns.put( "sfTitle", "Description");
        json.put( "aoColumns", columns);

        // Accumulate values
        for (row : rows) {
           JSONObject jrow = new JSONObject() ;
           jrow.accumulate("row", row.getName());
           jrow.accumulate("row",row.getDescription());
           json.put("aoData",jrow) ;
        }

System.out.println(json) ;

I think that's about right - you should be able to tweak to exactly what you want.

Doug
0
 
LVL 16

Assisted Solution

by:Valeri
Valeri earned 250 total points
ID: 39163807
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

770 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