0

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

1 Answer 1

2

Your json is not right. Change to this.

{"name":"Test","age":"12"}
Sign up to request clarification or add additional context in comments.

2 Comments

Great thanks... it works.. so we don't need to mention the class name in the JSON Object notation ??
No, we needn't . Just use the content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.