Solved

JSON manipulation in Java

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Node.js 11 77
Need a script that will create its own directory to dump logs into 7 49
Difference between Leaflet and MapBox? 5 45
Notepad++ Question 11 25
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This article discusses how to implement server side field validation and display customized error messages to the client.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

739 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