FastJson是阿里巴巴团队开发出一种开源工具。其作用可以讲JavaBean,集合,map集合转化为json格式的字符串。使用起来很方便。
首先下载阿里巴巴的fastjson-1.1.27.jar包,然后将其导入到项目当中。就可以实现json字符串与对象之间转换(toJsonString和parSEObject),转换过的对象可以通过getxxx(属性名)获取到对应的值;
============================================================================================================
下面是我写的几个测试的例子:
对象
@Test public void fun(){ Student st = new Student("Jim","110","USA"); String str = JSON.toJSONString(st); System.out.println(str); }
集合
@Test public void fun1(){ Student st1 = new Student("Jim1","1101","USA1"); Student st2 = new Student("Jim2","1102","USA2"); Student st3 = new Student("Jim3","1103","USA3"); List<Student> list = new ArrayList<Student>(); list.add(st1); list.add(st2); list.add(st3); String str = JSON.toJSONString(list); System.out.println(str); }
map集合
@Test public void fun4(){ Map<String,Student> map = new HashMap<String,Student>(); Student st1 = new Student("Jim1","USA3"); map.put("one",st1); map.put("two",st2); map.put("three",st3); String str = JSON.toJSONString(map); System.out.println(str); }原文链接:https://www.f2er.com/json/290022.html