日期:2014-05-16 浏览次数:20432 次
基于json-lib.jar包Json实例程序 1.JSONObject to DynaBean String json = "{name=\"json\",bool:true,int:1,double:2.2}"; JSONObject jsonObject = JSONObject.fromObject(json); //抽象的写法:DynaBean bean = (DynaBean) JSONSerializer.toJava( jsonObject ); Object bean = JSONObject.toBean(jsonObject); //Object bean1 = JSONSerializer.toJava(jsonObject); assertEquals(jsonObject.get("name"), PropertyUtils.getProperty(bean, "name")); assertEquals(jsonObject.get("bool"), PropertyUtils.getProperty(bean, "bool")); assertEquals(jsonObject.get("int"), PropertyUtils.getProperty(bean, "int")); assertEquals(jsonObject.get("double"), PropertyUtils.getProperty(bean, "double")); 2.JSONObject to JavaBean String json = "{name:\"zhangsan\",age:25,hight:1.72,sex:true}"; JSONObject jsonObject = JSONObject.fromObject(json); UserBean bean = (UserBean) JSONObject.toBean(jsonObject, UserBean.class); System.out.println(jsonObject); 理论上,这样就可以了,但时,有异常Caused by: java.lang.NoSuchMethodException: com.json.Json$UserBean.<init>() 3.JSONArray to List String json = "[\"first\",\"second\"]"; JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(json); List output = (List) JSONSerializer.toJava(jsonArray); 4.JSONArray to array String json = "[\"first\",\"second\"]"; JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(json); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setArrayMode(JsonConfig.MODE_OBJECT_ARRAY); Object[] output = (Object[]) JSONSerializer.toJava(jsonArray, jsonConfig); Object[] expected = new Object[] { "first", "second" }; ArrayAssertions.assertEquals(expected, output); 5.JSON 字符串 专为 JavaBean(刘慧斌demo 演示需要的jar包在附件里) String str="[{\"id\":\"328\",\"mestype\":\"inbox\"},{\"id\":\"327\",\"mestype\":\"inbox\"},{\"id\":\"279\",\"mestype\":\"already\"},{\"id\":\"278\",\"mestype\":\"already\"},{\"id\":\"277\",\"mestype\":\"already\"},{\"id\":\"310\",\"mestype\":\"inbox\"},{\"id\":\"308\",\"mestype\":\"inbox\"},{\"id\":\"305\",\"mestype\":\"inbox\"},{\"id\":\"304\",\"mestype\":\"inbox\"},{\"id\":\"303\",\"mestype\":\"inbox\"}]"; JSONArray jsonArray=(JSONArray) JSONSerializer.toJSON(str); List list=(List)JSONSerializer.toJava(jsonArray); for (Object obj: list) { JSONObject jsonObject = JSONObject.fromObject(obj); MessageBean bean = (MessageBean) JSONObject.toBean(jsonObject, MessageBean.class); String id=bean.getId()+""; String type=bean.getMestype(); System.out.println(id+" "+type); } System.out.println(list.size());
public class JsonDemo { private static JsonMapper mapper = JsonMapper.buildNonDefaultMapper(); 1、序列化对象/集合到Json字符串. @Test public void toJson() throws Exception { //Bean TestBean bean = new TestBean("A"); String beanString = mapper.toJson(bean); System.out.println("Bean:" + beanString); assertEquals("{"name":"A"}", beanString); //Map Map<String, Object> map = Maps.newLinkedHashMap(); map.put("name", "A"); map.put("age", 2); String mapString = mapper.toJson(map); System.out.println("Map:" + mapString); assertEquals("{"name":"A","age":2}", mapString); //List<String> List<String> stringList = Lists.newArrayList("A", "B", "C"); String listString = mapper.toJson(stringList); System.out.println("String List:" + listString); assertEquals("["A","B","C"]", listString); //List<Bean> List<TestBean> beanList = Lists.newArrayList(new TestBean("A"), new TestBean("B")); String beanListString = mapper.toJson(beanList); System.out.println("Bean List:" + beanListString); assertEquals("[{"name":"A"},{"name":"B"}]", beanListString); //Bean[] TestBean[] beanArray = new TestBean[] { new TestBean("A"), new TestBean("B") }; Stri