Solved

JSON manipulation in Java

Posted on 2013-05-13
2
284 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
Comment Utility
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
Comment Utility
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 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

10 Experts available now in Live!

Get 1:1 Help Now