json list互相转换例子(采用jackson)
http://blog.csdn.net/einarzhang/article/details/6182372
项目中需要用到json与java List之间的转换,在此写出来自己的方法,这里采用的是jackson。请先下载jackson相关包和apache cxf下的jettison-1.2.jar
由于没有过多的研究jackson,自己写的只是一个可行的方式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
Java代码 package com.test; public class TestPojo { private String name; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "TestPojo [name=" + name + ", sex=" + sex + "]"; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
Java代码 package com.test; import java.io.IOException; import java.io.StringWriter; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; public class JSONTest { /** * @param args */ @SuppressWarnings("rawtypes") public static void main(String[] args) { ObjectMapper mapper = new ObjectMapper(); StringWriter sw = null; JSONArray array = null; List<TestPojo> list = new ArrayList<TestPojo>(); List<TestPojo> list2 = new ArrayList<TestPojo>(); try { for (int i = 0; i < 10; i++) { TestPojo pojo1 = new TestPojo(); pojo1.setName(i + "11"); pojo1.setSex(i + "22"); list.add(pojo1); sw = new StringWriter(); //转换自定义类 mapper.writeValue(sw, pojo1); System.out.println(sw.getBuffer().toString()); } //转换List sw = new StringWriter(); mapper.writeValue(sw, list); System.out.println(sw.getBuffer().toString()); //从字符串得到包含数组, 再从数组的到元素, 并转换成自定义对象 array = new JSONArray(sw.toString()); for (int i = 0; i < array.length(); i++) { TestPojo pojoReverse = mapper.readValue(array.getString(i), TestPojo.class); System.out.println(pojoReverse); list2.add(pojoReverse); } System.out.println("list size = " + list.size()); System.out.println("list2 size = " + list2.size()); //转换Map Map<String, Object> map = new HashMap<String, Object>(); map.put("key1", "pandy"); map.put("date", new Date()); sw = new StringWriter(); mapper.writeValue(sw, map); System.out.println(sw.getBuffer().toString()); //从字符串得到Map对象 Map map1 = mapper.readValue(sw.getBuffer().toString(),HashMap.class); System.out.println(map1.get("key1")); System.out.println(map1.get("date")); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |