Solved

JSON manipulation in Java

Posted on 2013-05-13
2
287 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 21
ngStyle with an input variable doesn't work 14 30
Form with Modals 16 42
Change text to radio button and calendar form 2 32
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

932 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

17 Experts available now in Live!

Get 1:1 Help Now