0

我在处理这个 json 时遇到了问题。

{"directory": {
         "employees": {"employee": [
             {
                 "field": [
                     {
                         "content": "Charlotte Abbott",
                         "id": "displayName"
                     },
                     {
                         "content": "Charlotte",
                         "id": "firstName"
                     },

我将它转换成一个看起来像这样的类

@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;
    }

并且在序列化时并没有到达所有变量来插入数据。相反,我得到了所有的空值。但是,我得到了正确数量(数量)的 Directory 对象,所以我知道它已经达到了那么远。有人对我在这里做错了什么有一些见解吗?json就是它的样子,不是我设计的,而是它的使用方式。

4

2 回答 2

2

你必须使用一个相当奇怪的数据结构,但就是这样。

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());
    }
}

你需要一个Wrapper类来环绕Directory.

public class Wrapper {
    private Directory directory;

    public Directory getDirectory() {
        return directory;
    }

    public void setDirectory(Directory directory) {
        this.directory = directory;
    }
}

Directory班级。

public class Directory {
    @SerializedName("employees")
    private Employees employees;

    public Employees getEmployees() {
        return employees;
    }

    public void setEmployees(Employees employees) {
        this.employees = employees;
    }
}

Employees班级:

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班级:

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班级:

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;
    }
}

在此处使用 GSON 示例查看 JSON 到 Java 对象:http: //java.sg/parsing-a-json-string-into-an-object-with-gson-easily/

于 2012-11-06T01:47:57.187 回答
0

所以这就是它的工作原理,我做对了,我只是在放入数据后没有正确验证数据,所以我因为无法分配和存储它以供以后访问而丢失了它。

它很丑,但地图看起来像这样

DIRECTORY obj
{
    EMPLOYEES obj
    {
        List employee []
        {
            int id
            List fields[]
            {
                content
                id
            {     
        {    
    { 
{   
于 2012-11-06T01:44:13.007 回答