/**
* List集合转换成JSNO
*/
public static void getJsonToList() {
List<String> list = new ArrayList<String>();
list.add("first");
list.add("second");
JSONArray array = JSONArray.fromObject(list);
for (int i = 0; i < array.size(); i++)
{
System.out.println(array.get(i));
}
System.out.println(array);
}
/**
* Map集合转换成JSNO并且遍历JSNO对象
*/
public static void getJsonToMap() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "json");
map.put("bool", Boolean.TRUE);
map.put("int", new Integer(1));
map.put("arr", new String[] { "a", "b" });
map.put("func", "function(i){ return this.arr[i]; }");
JSONObject json = JSONObject.fromObject(map);
Iterator iter = json.keys();
while(iter.hasNext()) {
String str = (String)iter.next();
Object obj = json.get(str);
if(obj instanceof JSONArray) {
System.out.println(obj.getClass());
JSONArray array = (JSONArray)obj;
for (int i = 0; i < array.size(); i++)
{
System.out.println((String)array.get(i));
}
}
else if(obj instanceof Integer) {
System.out.println(obj.getClass());
System.out.println(((Integer)obj).toString());
}
else if(obj instanceof String) {
System.out.println(obj.getClass());
System.out.println(obj.toString());
}
else if(obj instanceof Boolean) {
System.out.println(obj.getClass());
System.out.println((Boolean)obj);
}
//System.out.println(obj.getClass());
}
System.out.println(json);
}
/**
* Bean转换成json代码
*/
public static void getJsonToBean() {
JavaBean bean = new JavaBean();
bean.setName("张三");
bean.setSex("男");
bean.setAge("20");
JavaBean bean2 = new JavaBean();
bean2.setName("李四");
bean2.setSex("女");
bean2.setAge("25");
JSONObject jsonObject = JSONObject.fromObject(bean);
System.out.println(jsonObject);
}
public static void getJsonToBeans() {
List list = new ArrayList();
JavaBean bean = new JavaBean();
bean.setName("张三");
bean.setSex("男");
bean.setAge("20");
JavaBean bean2 = new JavaBean();
bean2.setName("李四");
bean2.setSex("女");
bean2.setAge("25");
list.add(bean);
list.add(bean2);
JSONArray json = JSONArray.fromObject(list);
for (int i = 0; i < json.size(); i++)
{
JSONObject obj = (JSONObject)json.get(i);
Iterator iter = obj.keys();
while(iter.hasNext()) {
String str = (String)iter.next();
String value = (String)obj.get(str);
System.out.println("str:"+str+"---value:"+value);
}
}
}
/**
* 数组转换成JSON
*/
public static void getJsonToArray() {
boolean[] boolArray = new boolean[] { true, false, true };
JSONArray jsonArray = JSONArray.fromObject(boolArray);
System.out.println(jsonArray);
}
/**
* 一般数据转换成JSNO
*/
public static void getJsonToData() {
JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']" );
System.out.println(jsonArray);
}
/*public static void getJsonInfo() {
String receiveStr = "{'opType':'add','infostr':[{'infoId':'123456'},{'infoId':'000000'}]}";
JSONObject jsonObject = new JSONObject();
}*/
* List集合转换成JSNO
*/
public static void getJsonToList() {
List<String> list = new ArrayList<String>();
list.add("first");
list.add("second");
JSONArray array = JSONArray.fromObject(list);
for (int i = 0; i < array.size(); i++)
{
System.out.println(array.get(i));
}
System.out.println(array);
}
/**
* Map集合转换成JSNO并且遍历JSNO对象
*/
public static void getJsonToMap() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "json");
map.put("bool", Boolean.TRUE);
map.put("int", new Integer(1));
map.put("arr", new String[] { "a", "b" });
map.put("func", "function(i){ return this.arr[i]; }");
JSONObject json = JSONObject.fromObject(map);
Iterator iter = json.keys();
while(iter.hasNext()) {
String str = (String)iter.next();
Object obj = json.get(str);
if(obj instanceof JSONArray) {
System.out.println(obj.getClass());
JSONArray array = (JSONArray)obj;
for (int i = 0; i < array.size(); i++)
{
System.out.println((String)array.get(i));
}
}
else if(obj instanceof Integer) {
System.out.println(obj.getClass());
System.out.println(((Integer)obj).toString());
}
else if(obj instanceof String) {
System.out.println(obj.getClass());
System.out.println(obj.toString());
}
else if(obj instanceof Boolean) {
System.out.println(obj.getClass());
System.out.println((Boolean)obj);
}
//System.out.println(obj.getClass());
}
System.out.println(json);
}
/**
* Bean转换成json代码
*/
public static void getJsonToBean() {
JavaBean bean = new JavaBean();
bean.setName("张三");
bean.setSex("男");
bean.setAge("20");
JavaBean bean2 = new JavaBean();
bean2.setName("李四");
bean2.setSex("女");
bean2.setAge("25");
JSONObject jsonObject = JSONObject.fromObject(bean);
System.out.println(jsonObject);
}
public static void getJsonToBeans() {
List list = new ArrayList();
JavaBean bean = new JavaBean();
bean.setName("张三");
bean.setSex("男");
bean.setAge("20");
JavaBean bean2 = new JavaBean();
bean2.setName("李四");
bean2.setSex("女");
bean2.setAge("25");
list.add(bean);
list.add(bean2);
JSONArray json = JSONArray.fromObject(list);
for (int i = 0; i < json.size(); i++)
{
JSONObject obj = (JSONObject)json.get(i);
Iterator iter = obj.keys();
while(iter.hasNext()) {
String str = (String)iter.next();
String value = (String)obj.get(str);
System.out.println("str:"+str+"---value:"+value);
}
}
}
/**
* 数组转换成JSON
*/
public static void getJsonToArray() {
boolean[] boolArray = new boolean[] { true, false, true };
JSONArray jsonArray = JSONArray.fromObject(boolArray);
System.out.println(jsonArray);
}
/**
* 一般数据转换成JSNO
*/
public static void getJsonToData() {
JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']" );
System.out.println(jsonArray);
}
/*public static void getJsonInfo() {
String receiveStr = "{'opType':'add','infostr':[{'infoId':'123456'},{'infoId':'000000'}]}";
JSONObject jsonObject = new JSONObject();
}*/