如何转换json属性,如" oData.type"到java对象

时间:2017-04-12 12:41:02

标签: java gson

我正在尝试将以下JSON字符串转换为Java对象

String json = "{\"odata.type\":\"SP.Data.UserInfoItem\",\"odata.id\":\"479655bd-324f-4584-b6b21e6c1777ad6\",\"Id\":735,\"Name\":\"i:0#.f|membership|xxxxx@xxxxx.com\",\"Title\":\"xxxxx\"}";

convertStringToJavaPbject(json);

这是我的java方法

private static void convertStringToJavaPbject(String json) {

    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.create();

    JsonElement jElement = gson.fromJson(json,JsonElement.class);

    Auther auther = gson.fromJson(jElement, Auther.class);
    System.out.println(" Auther : ["+auther);

}

和我的Auther pojo是

public class Auther {

 private String odatatype;
 private String odataid;
 private String name;
 private String title;
 private int id;

public String getOdatatype() {
    return odatatype;
}
public void setOdatatype(String odatatype) {
    this.odatatype = odatatype;
}
public String getOdataid() {
    return odataid;
}
public void setOdataid(String odataid) {
    this.odataid = odataid;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
@Override
public String toString() {
    return "Auther [odatatype=" + odatatype + ", odataid=" + odataid
            + ", name=" + name + ", title=" + title + ", id=" + id + "]";
}

}

如何获得" odata.type"的价值?在我班上,因为我不能用" odata.type"声明变量?

0 个答案:

没有答案