需要导入gson包:gson-2.3.1.jar ,gson-2.3.1-sources.jar
测试实体类:
public class model {
private String Name1;
private String Name2;
private String Name3;
public model() {
super();
}
public model(String name1, String name2, String name3) {
super();
Name1 = name1;
Name2 = name2;
Name3 = name3;
}
@Override
public String toString() {
return "model [Name1=" + Name1 + ", Name2=" + Name2 + ", Name3="
+ Name3 + "]";
}
public String getName1() {
return Name1;
}
public void setName1(String name1) {
Name1 = name1;
}
public String getName2() {
return Name2;
}
public void setName2(String name2) {
Name2 = name2;
}
public String getName3() {
return Name3;
}
public void setName3(String name3) {
Name3 = name3;
}
}
1:实体类和json数据的相互转化:
public static void main(String[] args) {
Gson gson = new Gson();
model model1 = new model("value1","value2","value3");
String JsonMsg1 = gson.toJson(model1);
System.out.println("实体类——转——json: " + JsonMsg1);
model model2 = gson.fromJson("{\"Name1\":\"value1\",\"Name2\":\"value2\",\"Name3\":\"value3\"}", model.class);
//也可以直接用上面生成的json字符串JsonMsg1
//model model2 = gson.fromJson(JsonMsg1, model.class);
System.out.println("json——转——实体类" + model2.toString());
}
结果:
实体类——转——json: {"Name1":"value1","Name2":"value2","Name3":"value3"}
json——转——实体类model [Name1=value1, Name2=value2, Name3=value3]
2:map和json数据的相互转化:
public static void main(String[] args) {
Gson gson = new Gson();
Map<String,String> map1 = new HashMap<String, String>();
map1.put("Name1", "value1");
map1.put("Name2", "value2");
map1.put("Name3", "value3");
String JsonMsg2 = gson.toJson(map1);
System.out.println("map——转——json: " + JsonMsg2);
Map<String,String> map2 = gson.fromJson("{\"Name1\":\"value1\",\"Name2\":\"value2\",\"Name3\":\"value3\"}", Map.class);
System.out.println("json——转——map: " + map2);
}
结果:
map——转——json: {"Name3":"value3","Name2":"value2","Name1":"value1"}
json——转——map: {Name1=value1, Name2=value2, Name3=value3}
3:实体类list和json数据的相互转化:
public static void main(String[] args) {
Gson gson = new Gson();
ArrayList<model> list1 = new ArrayList<model>();
model model1 = new model("value11","value12","value13");
model model2 = new model("value21","value22","value23");
model model3 = new model("value31","value32","value33");
list1.add(model1);
list1.add(model2);
list1.add(model3);
String JsonMsg3 = gson.toJson(list1);
System.out.println("list——转——json: " + JsonMsg3);
ArrayList<model> list2 = (ArrayList<model>) gson.fromJson("[{\"Name1\":\"value1\",\"Name2\":\"value2\",\"Name3\":\"value3\"},{\"Name1\":\"value1\",\"Name2\":\"value2\",\"Name3\":\"value3\"},{\"Name1\":\"value31\",\"Name2\":\"value32\",\"Name3\":\"value33\"}]", List.class);
System.out.println("json——转——List: ");
for (Object temp : list2) {
System.out.print(gson.fromJson((temp).toString(), model.class) + "-");
}
}
结果:
list——转——json: [{"Name1":"value11","Name2":"value12","Name3":"value13"},{"Name1":"value21","Name2":"value22","Name3":"value23"},{"Name1":"value31","Name2":"value32","Name3":"value33"}]
json——转——List:
model [Name1=value1, Name2=value2, Name3=value3]-model [Name1=value1, Name2=value2, Name3=value3]-model [Name1=value31, Name2=value32, Name3=value33]-
创建修改实体类,创建另一个实体类:
model.java
public class model {
private String Name1;
private String Name2;
private String Name3;
private model2 model2 ;
public model() {
super();
}
public model(String name1, String name2, String name3,
com.json.test.model2 model2) {
super();
Name1 = name1;
Name2 = name2;
Name3 = name3;
this.model2 = model2;
}
@Override
public String toString() {
return "model [Name1=" + Name1 + ", Name2=" + Name2 + ", Name3="
+ Name3 + ", model2=" + model2 + "]";
}
public model2 getModel2() {
return model2;
}
public void setModel2(model2 model2) {
this.model2 = model2;
}
public String getName1() {
return Name1;
}
public void setName1(String name1) {
Name1 = name1;
}
public String getName2() {
return Name2;
}
public void setName2(String name2) {
Name2 = name2;
}
public String getName3() {
return Name3;
}
public void setName3(String name3) {
Name3 = name3;
}
}
model2.java
public class model2 {
private String Name4;
private String Name5;
public model2() {
super();
}
public model2(String name4, String name5) {
super();
Name4 = name4;
Name5 = name5;
}
@Override
public String toString() {
return "model2 [Name4=" + Name4 + ", Name5=" + Name5 + "]";
}
public String getName4() {
return Name4;
}
public void setName4(String name4) {
Name4 = name4;
}
public String getName5() {
return Name5;
}
public void setName5(String name5) {
Name5 = name5;
}
}
4:实体类嵌套另一个实体类 和 json 数据的相互转换:
public static void main(String[] args) {
Gson gson = new Gson();
model2 model4 = new model2("value4" , "value5");
model model1 = new model("value1","value2","value3",model4);
model1.setModel2(model4);
String JsonMsg4 = gson.toJson(model1);
System.out.println("实体类嵌套另一个实体类——转——json: " + JsonMsg4);
model model5 = gson.fromJson(JsonMsg4, model.class);
System.out.println("json——转——实体类嵌套另一个实体类" + model5.toString());
}
结果:
实体类嵌套另一个实体类——转——json: {"Name1":"value1","Name2":"value2","Name3":"value3","model2":{"Name4":"value4","Name5":"value5"}}
json——转——实体类嵌套另一个实体类model [Name1=value1, Name2=value2, Name3=value3, model2=model2 [Name4=value4, Name5=value5]]
再次修改实体类model.java
import java.util.ArrayList;
public class model {
private String Name1;
private String Name2;
private String Name3;
private ArrayList<model2> modelList ;
public model() {
super();
}
public model(String name1, String name2, String name3,
ArrayList<model2> modelList) {
super();
Name1 = name1;
Name2 = name2;
Name3 = name3;
this.modelList = modelList;
}
@Override
public String toString() {
return "model [Name1=" + Name1 + ", Name2=" + Name2 + ", Name3="
+ Name3 + ", modelList=" + modelList + "]";
}
public ArrayList<model2> getModelList() {
return modelList;
}
public void setModelList(ArrayList<model2> modelList) {
this.modelList = modelList;
}
public String getName1() {
return Name1;
}
public void setName1(String name1) {
Name1 = name1;
}
public String getName2() {
return Name2;
}
public void setName2(String name2) {
Name2 = name2;
}
public String getName3() {
return Name3;
}
public void setName3(String name3) {
Name3 = name3;
}
}
5:实体类嵌套另一个实体类list 和 json 数据的相互转换:
public static void main(String[] args) {
Gson gson = new Gson();
model2 model2 = new model2("value24" , "value25");
model2 model3 = new model2("value34" , "value35");
ArrayList<model2> list3 = new ArrayList<model2>();
list3.add(model2);
list3.add(model3);
model model1 = new model("value1","value2","value3",list3);
String JsonMsg5 = gson.toJson(model1);
System.out.println("实体类嵌套另一个实体类list——转——json: " + JsonMsg5);
model model7 = gson.fromJson(JsonMsg5, model.class);
System.out.println("json——转——实体类嵌套另一个实体类list: " + model7);
}
结果:
实体类嵌套另一个实体类list——转——json: {"Name1":"value1","Name2":"value2","Name3":"value3","modelList":[{"Name4":"value24","Name5":"value25"},{"Name4":"value34","Name5":"value35"}]}
json——转——实体类嵌套另一个实体类list: model [Name1=value1, Name2=value2, Name3=value3, modelList=[model2 [Name4=value4, Name5=value5], model2 [Name4=value64, Name5=value65]]]