需要jar包
commons-beanutils-1.7.0.jar
ezmorph-1.0.3.jar
json-lib-2.1.jar
commons-beanutils-1.7.0.jar
ezmorph-1.0.3.jar
json-lib-2.1.jar
public class Test {
public static void main(String[] args) {
//json to bean
String json = "{'value':'xx','row':1,'col':1}";
JSONObject jsonObject = JSONObject.fromObject(json);
JsonBean2 bean = (JsonBean2) JSONObject.toBean(jsonObject,
JsonBean2.class);
System.out.println(bean.getCol());
//1、boolArray to json
boolean[] boolArray = new boolean[] { true, false, true };
JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
System.out.println(jsonArray1);
//3、strList to json
List strList = new ArrayList();
strList.add("first");
strList.add("second");
JSONArray jsonArray2 = JSONArray.fromObject(strList);
System.out.println(jsonArray2);
//4、strArray to json
String strArray = "['json','is','easy']";
JSONArray jsonArray3 = JSONArray.fromObject(strArray);
System.out.println(jsonArray3);
//2、objList to json
List<JsonBean2> objList = new ArrayList<JsonBean2>();
JsonBean2 jb1 = new JsonBean2();
jb1.setCol(1);
jb1.setRow(1);
jb1.setValue("xx");
JsonBean2 jb2 = new JsonBean2();
jb2.setCol(2);
jb2.setRow(2);
jb2.setValue("");
objList.add(jb1);
objList.add(jb2);
JSONArray ja = JSONArray.fromObject(objList);
System.out.println(ja.toString());
//5、Objmap to json
Map<Integer, JsonBean2> map = new HashMap<Integer, JsonBean2>();
map.put(jb1.getCol(), jb1);
map.put(jb2.getCol(), jb2);
JSONObject jsonMapObj = JSONObject.fromObject(map);
System.out.println(jsonMapObj);
//6、StringMap to json
Map map1 = new HashMap();
map1.put("name", "json");
map1.put("bool", Boolean.TRUE);
map1.put("int", new Integer(1));
map1.put("arr", new String[] { "a", "b" });
map1.put("func", "function(i){ return this.arr[i]; }");
JSONObject jsonMap = JSONObject.fromObject(map1);
System.out.println(jsonMap);
JSONObject jsonMap1 = JSONObject.fromObject(map1);
jsonMap1.put("jsonMap1", map1);
System.out.println(jsonMap1);
String json1 = "{'jsonMap1':[{'col':1,'value':'xx','row':1},{'col':2,'value':'','row':2}]}";
JSONObject jsonObject2 = JSONObject.fromObject(json1);
JSONArray array = jsonObject2.getJSONArray("jsonMap1");
List<MorphDynaBean> list = JSONArray.toList(array);
System.out.println(list.get(0).get("col"));
try {
PropertyUtils.copyProperties(list.get(0), jb2);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(jb2.getCol());
System.out.println("value: "+jb2.getValue());
}
}
//json to list
String jsonstr = "{'users':[{'id':'1','name':'user1'},{'id':'2,'name':'user2'}]}"
JSONObject jsonObject = JSONObject.fromObject(jsonstr );
JSONArray jsonArray = jsonObject.getJSONArray("users");
List<MorphDynaBean> list = jsonArray.toList(jsonArray);
for (MorphDynaBean morphDynaBean : list) {
System.out.println((Integer) morphDynaBean.get("id"));
System.out.println((String) morphDynaBean.get("name"));
}