I am having trouble with this json.
{"directory": {
"employees": {"employee": [
{
"field": [
{
"content": "Charlotte Abbott",
"id": "displayName"
},
{
"content": "Charlotte",
"id": "firstName"
},
I am casting it into a class that looks like this
@SerializedName("directory")
public Directory directory;
public class Directory
{
@SerializedName("employees")
public Employees employees;
}
public class Employees
{
@SerializedName("employee")
public List<Employee> employee;
}
public class Employee
{
@SerializedName("field")
public List<Fields> fields;
@SerializedName("id")
public String employeeId;
}
public class Fields
{
@SerializedName("content")
public String content;
@SerializedName("id")
public String label;
}
And it is not reaching all the variables to insert the data when it serializes. Instead I am getting all nulls. I am however getting the right amount (number) of Directory objects so I know it is reaching that far. Anyone have some insight on what I am doing wrong here? The json is the way it is, I didn't design it, but it is how it is used.
Quite a weird data structure you have to work with, but here is it.
public class App {
public static void main(String[] args) {
Gson gson = new Gson();
String jsonString = "{\"directory\": {\"employees\": {\"employee\": [{\"field\": [{\"content\": \"Charlotte Abbott\",\"id\": \"displayName\"},{\"content\": \"Charlotte\",\"id\": \"firstName\"}]}]}}}";
Wrapper obj = (Wrapper) gson.fromJson(jsonString, Wrapper.class);
System.out.println(obj.getDirectory().getEmployees().getEmployeeList()
.get(0).getFieldList().get(0).getContent());
}
}
You need a Wrapper
class to wrap around Directory
.
public class Wrapper {
private Directory directory;
public Directory getDirectory() {
return directory;
}
public void setDirectory(Directory directory) {
this.directory = directory;
}
}
Directory
class.
public class Directory {
@SerializedName("employees")
private Employees employees;
public Employees getEmployees() {
return employees;
}
public void setEmployees(Employees employees) {
this.employees = employees;
}
}
Employees
class:
public class Employees {
@SerializedName("employee")
private List<Employee> employeeList;
public List<Employee> getEmployeeList() {
return employeeList;
}
public void setEmployeeList(List<Employee> employeeList) {
this.employeeList = employeeList;
}
}
Employee
class:
public class Employee {
@SerializedName("field")
private List<Field> fieldList;
public List<Field> getFieldList() {
return fieldList;
}
public void setFieldList(List<Field> fieldList) {
this.fieldList = fieldList;
}
}
Field
class:
public class Field {
@SerializedName("content")
private String content;
@SerializedName("id")
private String id;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
See a JSON to Java Object using GSON example here: http://java.sg/parsing-a-json-string-into-an-object-with-gson-easily/
So here is how it worked out, i had it right, i just wasn't validating the data correctly after it was being put in, and so I was losing it by not being able to assign it and store it for later access.
It is ugly but the map looks like this
DIRECTORY obj
{
EMPLOYEES obj
{
List employee []
{
int id
List fields[]
{
content
id
{
{
{
{
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.