三者效率比较:jackson很接近fastjson, gson解析最全,点击各自链接,可以查看更详细的使用手册
jsckson基本使用
引用
compile 'com.fasterxml.jackson.core:jackson-databind:2.8.5'
compile 'com.fasterxml.jackson.core:jackson-core:2.8.5'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.8.5'
使用
ObjectMapper mapper = new ObjectMapper();
Person person = new Person();
mapper.writeValue(new File("/a/path/to/person.json"), person); // write to file
String jsonStr = mapper.writeValueAsString(person); // write to string
writeValue: 写入文件
writeValueAsString: 写入字符串
person1 = mapper.readValue(new URL("https://api.myjson.com/bins/hoh4j"), Person.class);
//read from a string
String personJsonStr = "{\"firstname\":\"John\",\"lastname\":\"Doe\"}";
person2 = mapper.readValue(personJsonStr, Person.class);
//read from a file
person3 = mapper.readValue(new File("/a/path/to/person.json"), Person.class);
readValue:可以从url,file对象,string中解析到类
fastjson基本使用
引用 compile'com.alibaba:fastjson:VERSION_CODE'
Map<String, User> map = JSON.parseObject(genericJson, new TypeReference<Map<String, User>>() {});
Group group = JSON.parseObject(json, Group.class);
解析数据为集合或者类
String jsonString = JSON.toJSONString(group);
解析为字符串
Gson基本使用
implementation'com.google.code.gson:gson:2.8.5'
String jsonString = JSON.toJSONString(group);
Group group = JSON.parseObject(json, Group.class);
Map<String, User> map = JSON.parseObject(genericJson, new TypeReference<Map<String, User>>() {});
网友评论