Java中已经有不少的JSON工具,功能都很强大,可配置性很强。
FastJSON虽然功能不如Json Lib等强大,但将对象转换为JSON,及反向操作都没有这个类简单。
FastJSON,速度快,只需一行代码即可将类轻松转换为JSON字符串。
FastJSON:
import com.alibaba.fastjson.JSON;public class FastJson { public static void main(String[] args) { User u = new User("Biao", 23); System.out.println(JSON.toJSONString(u)); String js = "{\"name\":\"Bi\\\"ao\",\"age\":23}"; u = JSON.parseObject(js, User.class); System.out.println(u); }}public class User { public String name; public int age; public User() { } public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return name + ": " + age; }}