英文:
Unable to convert to java Object using GSON
问题
I have the following JSON , which i need to convert to Java Object
[{
"emp": {
"name": "pavan"
},
"dept": {
"deptName": "cse"
}
}]
I am trying to convert this JSON to Java Object using GSON Framework
public class Root {
private Data data ;
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
}
public class Data {
private Emp emp;
private Dept dept;
public void setEmp(Emp emp){
this.emp = emp;
}
public Emp getEmp(){
return this.emp;
}
public void setDept(Dept dept){
this.dept = dept;
}
public Dept getDept(){
return this.dept;
}
}
public class Emp
{
private String name;
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
public class Dept
{
private String deptName;
public void setDeptName(String deptName){
this.deptName = deptName;
}
public String getDeptName(){
return this.deptName;
}
}
This is my Test class
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
String json = "[{\r\n" +
" \"emp\": {\r\n" +
" \"name\": \"pavan\"\r\n" +
" },\r\n" +
" \"dept\": {\r\n" +
" \"deptName\": \"cse\"\r\n" +
" }\r\n" +
"}]";
Gson gson = new Gson();
Root[] root = gson.fromJson(json, Root[].class);
System.out.println(root[0].getData());
}
}
I am using GSON to convert json to Java Object
I am getting null when i do the getData , can anybody please tell me why the conversion is not happening
<details>
<summary>英文:</summary>
I have the following JSON , which i need to convert to Java Object
[{
"emp": {
"name": "pavan"
},
"dept": {
"deptName": "cse"
}
}]
I am trying to convert this JSON to Java Object using GSON Framework
public class Root {
private Data data ;
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
}
public class Data {
private Emp emp;
private Dept dept;
public void setEmp(Emp emp){
this.emp = emp;
}
public Emp getEmp(){
return this.emp;
}
public void setDept(Dept dept){
this.dept = dept;
}
public Dept getDept(){
return this.dept;
}
}
public class Emp
{
private String name;
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
public class Dept
{
private String deptName;
public void setDeptName(String deptName){
this.deptName = deptName;
}
public String getDeptName(){
return this.deptName;
}
This is my Test class
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
String json = "[{\r\n" +
" \"emp\": {\r\n" +
" \"name\": \"pavan\"\r\n" +
" },\r\n" +
" \"dept\": {\r\n" +
" \"deptName\": \"cse\"\r\n" +
" }\r\n" +
"}]";
Gson gson = new Gson();
Root[] root = gson.fromJson(json, Root[].class);
System.out.println(root[0].getData());
}
I am using GSON to convert json to Java Object
I am getting null when i do the getData , can anybody please tell me why the conversion is not happening
</details>
# 答案1
**得分**: 1
将代码中的部分翻译如下:
原文:
The Root class is redundant.
Change
Root[] root = gson.fromJson(json, Root[].class);
to
Data[] data = gson.fromJson(json, Data[].class);
翻译:
Root类是多余的。
更改
Root[] root = gson.fromJson(json, Root[].class);
为
Data[] data = gson.fromJson(json, Data[].class);
<details>
<summary>英文:</summary>
The Root class is redundant.
Change
Root[] root = gson.fromJson(json, Root[].class);
to
Data[] data= gson.fromJson(json, Data[].class);
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论