在开发中我们经常通过Gson来解析字符串,转化为所需要的Java对象。使用到它的地方很多,常用的几个方法总结如下:
1.字符串转实体类
public <T> T fromJson(JsonElement json,
Class<T> classOfT)
throws JsonSyntaxException
This method deserializes the Json read from the specified parse tree into an object of the specified type.
示例:
/**
* str ---> bean
*/
private void str2bean(){
String str = "{'address':'xxx街xxx号','deptList':[{'employeeNum':'0','name':'部门0','task':'研发0'},{'employeeNum':'10','name':'部门1','task':'研发1'},{'employeeNum':'20','name':'部门2','task':'研发2'}],'name':'xxx公司'}";
Company cpy = new Gson().fromJson(str, Company.class);
Log.e("bean", cpy.toString());
}
2.实体类转字符串
String toJson(Object src)
This method serializes the specified object into its equivalent Json representation.
示例:
/**
* bean ---> Json字符串
*/
private void bean2str(){
Company cpy = new Company();
List<Department> list = new ArrayList<Department>();
for(int i=; i<3;i++){
Department dpt = new Department();
dpt.setName("部门"+i);
dpt.setEmployeeNum("" + i * );
dpt.setTask("研发"+i);
list.add(dpt);
}
cpy.setName("xxx公司");
cpy.setAddress("xxx街xxx号");
cpy.setDeptList(list);
String str = new Gson().toJson(cpy);
Log.e("Company", str);
}
// {"address":"xxx街xxx号","deptList":[{"employeeNum":"0","name":"部门0","task":"研发0"},{"employeeNum":"10","name":"部门1","task":"研发1"},{"employeeNum":"20","name":"部门2","task":"研发2"}],"name":"xxx公司"}
3.字符串转JsonObject
JsonElement parse(String json)
Parses the specified JSON string into a parse tree
示例:
/**
* str ---> JsonObject
*/
private void str2JsonObject() {
String str = "{'address':'xxx街xxx号','deptList':[{'employeeNum':'0','name':'部门0','task':'研发0'},{'employeeNum':'10','name':'部门1','task':'研发1'},{'employeeNum':'20','name':'部门2','task':'研发2'}],'name':'xxx公司'}";
JsonObject returnData = new JsonParser().parse(str).getAsJsonObject();
Log.e("JsonObject", returnData.toString());
//{"address":"xxx街xxx号","deptList":[{"employeeNum":"0","name":"部门0","task":"研发0"},{"employeeNum":"10","name":"部门1","task":"研发1"},{"employeeNum":"20","name":"部门2","task":"研发2"}],"name":"xxx公司"}
}
4.字符串转JsonArray
/**
* str ---> JsonArray
*/
private void str2JsonArray() {
String arrStr = "[{'employeeNum':'0','name':'部门0','task':'研发0'},{'employeeNum':'10','name':'部门1','task':'研发1'},{'employeeNum':'20','name':'部门2','task':'研发2'}]";
JsonArray jsArray = new JsonParser().parse(arrStr).getAsJsonArray();
Log.e("JsonArray", jsArray.get().getAsJsonObject().get("name").getAsString());
}
5.字符串转JsonArray实体
/**
* str ---> JsonArrayBean
*/
private void str2JsonArrayBean(){
String arrStr = "[{'employeeNum':'0','name':'部门0','task':'研发0'},{'employeeNum':'10','name':'部门1','task':'研发1'},{'employeeNum':'20','name':'部门2','task':'研发2'}]";
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonArray jsArray = parser.parse(arrStr).getAsJsonArray();
ArrayList<Department> dptList = new ArrayList<Department>();
for(JsonElement obj : jsArray){
Department dpt = gson.fromJson( obj , Department.class);
dptList.add(dpt);
Log.e("Dpt", dpt.toString());
}
}
涉及到的测试Java实体如下:
package com.example.gsontest;
import java.util.List;
public class Company {
private String name;
private String address;
private List<Department> deptList;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public List<Department> getDeptList() {
return deptList;
}
public void setDeptList(List<Department> deptList) {
this.deptList = deptList;
}
}
package com.example.gsontest;
public class Department {
private String name;
private String employeeNum;
private String task;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmployeeNum() {
return employeeNum;
}
public void setEmployeeNum(String employeeNum) {
this.employeeNum = employeeNum;
}
public String getTask() {
return task;
}
public void setTask(String task) {
this.task = task;
}
}