package org.json.xuxw;
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;
import net.sf.json.xml.XMLSerializer;
public class JsonDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// 轉(zhuǎn)化數(shù)組
boolean[] booleanArray = new boolean[]{true,false,true};
JSONArray jsonArray = JSONArray.fromObject(booleanArray);
//System.out.println(jsonArray);
// 轉(zhuǎn)化Cellection
List<String> list = new ArrayList<String>();
list.add("第一個");
list.add("第二個");
JSONArray jsonArrayList = JSONArray.fromObject(list);
//System.out.println(jsonArrayList);
//or
JSONArray jsonA = JSONArray.fromObject("['ok','now','love']");
//System.out.println(jsonA);
// 轉(zhuǎn)化對象
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "langw");
map.put("age", 28);
map.put("boolean", Boolean.TRUE);
map.put("attr", new String[]{"a","b","c"});
map.put("int", new Integer(1));
JSONObject jsonObject = JSONObject.fromObject(map);
//System.out.println(jsonObject);
// 轉(zhuǎn)化類
EntityTest et = new EntityTest("xuxw","123",10,new boolean[]{true,false});
JSONObject jsonObjEt = JSONObject.fromObject(et);
//System.out.println(jsonObjEt);
// Json 到 Bean
String jsonString = "{'age':10,'bollArray':[true,false],'password':'123','username':'xuxw'}";
JSONObject jbect = JSONObject.fromObject(jsonString);
EntityTest resultObj = (EntityTest)JSONObject.toBean(jbect,EntityTest.class);
//System.out.println(resultObj.getPassword());
//json to xml
String jsonString_a = "{'param':{'age':10,'bollArray':[true,false],'password':'123','username':'xuxw'}}";
JSONObject jbect_a = JSONObject.fromObject(jsonString);
XMLSerializer xMLSerializer = new XMLSerializer();
String xml1 = xMLSerializer.write(jbect_a);
System.out.println(xml1);
}
}
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報(bào)。