sasikals26
sasikals26

Reputation: 865

Cnverting JSON to Java Object using GSON

Can you please let me know what went wrong in the below sample.

Employee.java

public class Employee {

private String name;
public String getName() {
    return name;
}
public String getAge() {
    return age;
}
private String age;

}

JsontoJava.java

import com.google.gson.Gson;


public class JsontoJava {

public static void main(String ar[]){
    Gson gson = new Gson();
    String json = "{\"Employee\":[{\"name\":\"Test\", \"age\":\"12\"}]}";
    Employee staff = gson.fromJson(json, Employee.class);
    System.out.println("Name : "+staff.getName());
}

}

Unfortunately getting the wrong output:

Name :null

Upvotes: 0

Views: 316

Answers (1)

Jerry Zhu
Jerry Zhu

Reputation: 46

Your json is not right. Change to this.

{"name":"Test","age":"12"}

Upvotes: 2

Related Questions