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
json-lib-2.2.2-jdk15.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();
myBean1.setId(1);
myBean1.setName("a");

MyBean myBean2 = new MyBean();
myBean2.setId(2);
myBean2.setName("b");

List mybeanList = new ArrayList();
mybeanList.add(myBean1);
mybeanList.add(myBean2);

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);
}
}

======= MyBean.java =======

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;
}


}


===== runtTest.java =========


public class runTest {

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

}

}

9 comments:

Raghu said...

Excellent, Simple and Complete Post !

Thank you for ur post. :)

Ron said...

thanks
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": "http://tn.gov.in/Person/41" } ,
"fname": { "type": "literal" , "value": "Gayathri" } ,
"lname": { "type": "literal" , "value": "Vasudevan" }
} ,
{
"s": { "type": "uri" , "value": "http://tn.gov.in/Person/37" } ,
"fname": { "type": "literal" , "value": "Magesh" } ,
"lname": { "type": "literal" , "value": "Vasudevan" }
} ,
{
"s": { "type": "uri" , "value": "http://tn.gov.in/Person/39" } ,
"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...

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: http://www.youtube.com/watch?v=MN36K29q7_Q

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

yanmaneee said...

air max 270
adidas nmd r1
lebron 15
jordan shoes
adidas yeezy boost
michael kors
retro jordans
fila shoes
michael kors outlet
christian louboutin shoes