Tuesday, 15 July 2008

JSON : Array List <> JSON 2

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 testMain {

/**
* @param args
*/
public static void main(String[] args) {

MyBean myBean = new MyBean();
myBean.setId(1);
myBean.setName("one");
MyBean myBean2 = new MyBean();
myBean2.setId(2);
myBean2.setName("two");

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

JSONArray jsonArray = JSONArray.fromObject(mybeanList);
System.out.println("mybean list -----"+jsonArray);

Map map0 = new HashMap();
map0.put("success", true);

JSONObject mapObject = JSONObject.fromObject(map0);
System.out.println("map0 - "+mapObject);

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

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

}

}

No comments: