Sunday, 5 October 2008

Simple List to Json example

Download 6 package jar files

commons-lang 2.3
commons-beanutils 1.7.0
commons-collections 3.2
commons-logging 1.1
ezmorph 1.0.4.jar

Download all

=== Test source from List to Json ====
============== JsonTest =======================
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonTest {

public void Bean2Json(){
MyBean myBean1 = new MyBean();

MyBean myBean2 = new MyBean();

List mybeanList = new ArrayList();

JSONArray jsonArray = JSONArray.fromObject(mybeanList);
System.out.println("==== : "+jsonArray);

Map map = new HashMap();
map.put("beanlist", jsonArray);

JSONObject jsonObject = JSONObject.fromObject(map);
System.out.println("==== json :"+jsonObject);

======= =======

public class MyBean {

private int Id;
private String Name;

public int getId() {
return Id;
public void setId(int id) {
Id = id;
public String getName() {
return Name;
public void setName(String name) {
Name = name;


===== =========

public class runTest {

* @param args
public static void main(String[] args) {
JsonTest jsonTest = new JsonTest();




Raghu said...

Excellent, Simple and Complete Post !

Thank you for ur post. :)

Ron said...

works fine


Anonymous said...

hey can u help me with JSON datgrid in extjs..pls..

my json output from the servlet is :

"head": {
"vars": [ "s" , "fname" , "lname" ]
} ,
"results": {
"bindings": [
"s": { "type": "uri" , "value": "" } ,
"fname": { "type": "literal" , "value": "Gayathri" } ,
"lname": { "type": "literal" , "value": "Vasudevan" }
} ,
"s": { "type": "uri" , "value": "" } ,
"fname": { "type": "literal" , "value": "Magesh" } ,
"lname": { "type": "literal" , "value": "Vasudevan" }
} ,
"s": { "type": "uri" , "value": "" } ,
"fname": { "type": "literal" , "value": "Vasudevan " } ,
"lname": { "type": "literal" , "value": "Srinivasan" }

my problem is i am not getting my grid rendered, soemtime servlet not called , and sometimes an empty grid..!

Kinjan Ajudiya said...

yes mashy .. i got same problem...i got jsonarray from arraylist but there is problem in parsing that jsonarray to grid...pls some one help me ...

Anonymous said... - Learn how to turn $500 into $5,000 in a month!

[url=]Make Money Online[/url] - The Secret Reveled with Binary Option

Binary Options is the way to [url=]make money[/url] securely online

Anonymous said...

Good post. I learn something new and challenging on blogs I stumbleupon on a daily basis.

It's always exciting to read through content from other authors and practice something from other web sites.

Also visit my homepage; binary options strategy

Anonymous said...

With havin so much content do you ever run into any issues of plagorism or copyright infringement?
My website has a lot of unique content I've either written myself or outsourced but it seems a lot of it is popping it up all over the web without my authorization. Do you know any techniques to help prevent content from being stolen? I'd definitely appreciate it.

My site:

Anonymous said...

Incredible quest there. What happened after? Thanks!

My weblog: m88

Anonymous said...

If, for iPad, or any other thing about using online banking
or empire four kingdoms cheats credit/debit
card. Billions of subscribers are now asking for more.
It requires you to choose from, and then. In Spy Mouse
and you can access to all the documentations on a mobile device for making real-time interactive content.
You would have been lost with them and can use this app you wish to play this Barbie cooking game.

Have a look at my web blog empire four kingdoms cheats yahoo