无法使用GSON将其转换为Java对象。

huangapple go评论79阅读模式
英文:

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 

    [{
    	&quot;emp&quot;: {
    		&quot;name&quot;: &quot;pavan&quot;
    	},
    	&quot;dept&quot;: {
    		&quot;deptName&quot;: &quot;cse&quot;
    	}
    }]

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 = &quot;[{\r\n&quot; + 
    				&quot;	\&quot;emp\&quot;: {\r\n&quot; + 
    				&quot;		\&quot;name\&quot;: \&quot;pavan\&quot;\r\n&quot; + 
    				&quot;	},\r\n&quot; + 
    				&quot;	\&quot;dept\&quot;: {\r\n&quot; + 
    				&quot;		\&quot;deptName\&quot;: \&quot;cse\&quot;\r\n&quot; + 
    				&quot;	}\r\n&quot; + 
    				&quot;}]&quot;;
    		
    		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>



huangapple
  • 本文由 发表于 2020年8月6日 23:32:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63286960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定