Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

JSON manipulation in Java

Posted on 2013-05-13
2
Medium Priority
?
323 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
[X]
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
2 Comments
 
LVL 28

Accepted Solution

by:
dpearson earned 750 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 750 total points
ID: 39163807
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

636 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