Reputation: 865
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
Reputation: 46
Your json is not right. Change to this.
{"name":"Test","age":"12"}
Upvotes: 2