Gson反序列化问题

发布于 2024-12-01 03:53:03 字数 7472 浏览 0 评论 0原文

我需要使用 gson 反序列化 JSON 的帮助。

我有这个 JSON:

    {
           "result":{
              "grid":{
                 "javaClass":"com.company.collections.GridSummary",
                 "page":{
                    "list":[
                       {
                          "javaClass":"com.company.services.issue.IssueSummary",
                          "sprintName":"",
                          "assignedTo":"dannyd",
                          "numChildren":-2147483648,
                          "startDate":{
                             "javaClass":"java.util.Date",
                             "time":1.302881465e12
                          },
                          "title":"QA work done on 1895747 - DD",
                          "hasClickPath":false,
                          "sprintId":"",
                          "productName":"",
                          "ownerId":"1895747",
                          "customFollowUp":"888-888-5555",
                          "assigneeFirstName":"Danny",
                          "lastActivityDate":{
                             "javaClass":"java.util.Date",
                             "time":1.302888278e12
                          },
                          "id":"1895885",
                          "global":false,
                          "timeSpent":0,
                          "assignee":{
                             "javaClass":"com.company.services.contact.ContactSummary",
                             "isSem":false,
                             "subType":0,
                             "doNotCampaign":false,
                             "status":0,
                             "rating":0,
                             "firstName":"Danny",
                             "salesProcess":{
                                "editable":false,
                                "javaClass":"com.company.modules.salesrules.SalesProcessSummary",
                                "rules":{
                                   "list":[

                                   ],
                                   "javaClass":"java.util.ArrayList"
                                }
                             },
                             "lastName":"D",
                             "doNotEmail":false,
                             "doNotCall":false,
                             "stage":0,
                             "isRemoved":false
                          },
                          "priority":3,
                          "jiraIdRemoved":false,
                          "read":false,
                          "productPlatform":"",
                          "filesAttached":-2147483648,
                          "parentId":"305599",
                          "type":72,
                          "productId":"",
                          "createdBy":"dannyd",
                          "status":2,
                          "productAppGroup":"",
                          "reporterId":"c8035a744046382000f0474c07d7ed34",
                          "jiraId":"",
                          "projectId":"",
                          "removed":false,
                          "assigneeLastName":"D",
                          "description":"",
                          "clientId":"CLIENTID",
                          "jiraSprintName":"",
                          "projectName":"",
                          "customFollowUpType":"Other"
                       }
                    ],
                    "javaClass":"java.util.ArrayList"
                 },
                 "totalCount":1183
              },
              "javaClass":"com.company.blocks.issues.commands.SearchIssues",
              "accountPlatform":"7.0"
           }
        }

和这个代码:

    import com.google.gson.Gson;
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.ArrayList;


    /**
     *
     * @author Danny
     */
    public class Deserialize {

        public void main() throws FileNotFoundException {
            BufferedReader json = new BufferedReader(new FileReader("C:\\Users\\dannyd\\Desktop\\jsonResults.txt"));
            Gson gson = new Gson();
            MyContainer container = gson.fromJson(json, MyContainer.class);
        }
    }

    class MyContainer {

        private Result result = new Result();

        //getters & setters
    }

    class Result {

        private Grid grid = new Grid();
        private String javaClass;
        private String accountPlatform;

        //getters & setters
    }

    class Grid {

        private String javaClass;
        private Page page = new Page();
        private Integer totalCount;

        //getters & setters
    }

    class Page {

        private ArrayList<PageList> list;
        private String JavaClass;

        //getters & setters
    }

    class PageList {

        private String javaClass;
        private String sprintName;
        private String assignedTo;
        private Integer numChildren;
        private D_DateObject startDate = new D_DateObject();
        private String title;
        private Boolean hasClickPath;
        private String sprintId;
        private String productName;
        private String ownerId;
        private String customFollowUp;
        private String assigneeFirstName;
        private D_DateObject lastActivityDate = new D_DateObject();
        private String id;
        private Boolean global;
        private Integer timeSpent;
        private Assignee assignee = new Assignee();
        private Integer priority;
        private Boolean jiraIdRemoved;
        private Boolean read;
        private String productPlatform;
        private Integer filesAttached;
        private String parentId;
        private Integer type;
        private String productId;
        private String createdBy;
        private Integer status;
        private String productAppGroup;
        private String reporterId;
        private String jiraId;
        private String projectId;
        private Boolean removed;
        private String assigneeLastName;
        private String description;
        private String clientId;
        private String jiraSprintName;
        private String projectName;
        private String customFollowUpType;

        //getters & setters
    }

    class D_DateObject {

        private String javaClass;
        private Integer time;

        //getters & setters
    }

    class Assignee {

        private String javaClass;
        private Boolean isSem;
        private Integer subType;
        private Boolean doNotCampaign;
        private Integer status;
        private Integer rating;
        private String firstName;
        private SalesProcess salesProcess = new SalesProcess();
        private String lastName;
        private Boolean doNotEmail;
        private Boolean doNotCall;
        private Integer stage;
        private Boolean isRemoved;

        //getters & setters
    }

    class SalesProcess {

        private Boolean editable;
        private String javaClass;
        private Rules rules;

        //getters & setters
    }

    class Rules {

        private AList list = new AList();
        private String javaClass;
        //getters & setters
    }

    class AList {
    }

似乎 gson 没有在任何类中设置任何变量。我做错了什么吗? JSON 是从不同的位置检索的,我无权更改它。预先感谢您的任何帮助。

I need help with deserializing JSON with gson.

I have this JSON:

    {
           "result":{
              "grid":{
                 "javaClass":"com.company.collections.GridSummary",
                 "page":{
                    "list":[
                       {
                          "javaClass":"com.company.services.issue.IssueSummary",
                          "sprintName":"",
                          "assignedTo":"dannyd",
                          "numChildren":-2147483648,
                          "startDate":{
                             "javaClass":"java.util.Date",
                             "time":1.302881465e12
                          },
                          "title":"QA work done on 1895747 - DD",
                          "hasClickPath":false,
                          "sprintId":"",
                          "productName":"",
                          "ownerId":"1895747",
                          "customFollowUp":"888-888-5555",
                          "assigneeFirstName":"Danny",
                          "lastActivityDate":{
                             "javaClass":"java.util.Date",
                             "time":1.302888278e12
                          },
                          "id":"1895885",
                          "global":false,
                          "timeSpent":0,
                          "assignee":{
                             "javaClass":"com.company.services.contact.ContactSummary",
                             "isSem":false,
                             "subType":0,
                             "doNotCampaign":false,
                             "status":0,
                             "rating":0,
                             "firstName":"Danny",
                             "salesProcess":{
                                "editable":false,
                                "javaClass":"com.company.modules.salesrules.SalesProcessSummary",
                                "rules":{
                                   "list":[

                                   ],
                                   "javaClass":"java.util.ArrayList"
                                }
                             },
                             "lastName":"D",
                             "doNotEmail":false,
                             "doNotCall":false,
                             "stage":0,
                             "isRemoved":false
                          },
                          "priority":3,
                          "jiraIdRemoved":false,
                          "read":false,
                          "productPlatform":"",
                          "filesAttached":-2147483648,
                          "parentId":"305599",
                          "type":72,
                          "productId":"",
                          "createdBy":"dannyd",
                          "status":2,
                          "productAppGroup":"",
                          "reporterId":"c8035a744046382000f0474c07d7ed34",
                          "jiraId":"",
                          "projectId":"",
                          "removed":false,
                          "assigneeLastName":"D",
                          "description":"",
                          "clientId":"CLIENTID",
                          "jiraSprintName":"",
                          "projectName":"",
                          "customFollowUpType":"Other"
                       }
                    ],
                    "javaClass":"java.util.ArrayList"
                 },
                 "totalCount":1183
              },
              "javaClass":"com.company.blocks.issues.commands.SearchIssues",
              "accountPlatform":"7.0"
           }
        }

and this code:

    import com.google.gson.Gson;
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.ArrayList;


    /**
     *
     * @author Danny
     */
    public class Deserialize {

        public void main() throws FileNotFoundException {
            BufferedReader json = new BufferedReader(new FileReader("C:\\Users\\dannyd\\Desktop\\jsonResults.txt"));
            Gson gson = new Gson();
            MyContainer container = gson.fromJson(json, MyContainer.class);
        }
    }

    class MyContainer {

        private Result result = new Result();

        //getters & setters
    }

    class Result {

        private Grid grid = new Grid();
        private String javaClass;
        private String accountPlatform;

        //getters & setters
    }

    class Grid {

        private String javaClass;
        private Page page = new Page();
        private Integer totalCount;

        //getters & setters
    }

    class Page {

        private ArrayList<PageList> list;
        private String JavaClass;

        //getters & setters
    }

    class PageList {

        private String javaClass;
        private String sprintName;
        private String assignedTo;
        private Integer numChildren;
        private D_DateObject startDate = new D_DateObject();
        private String title;
        private Boolean hasClickPath;
        private String sprintId;
        private String productName;
        private String ownerId;
        private String customFollowUp;
        private String assigneeFirstName;
        private D_DateObject lastActivityDate = new D_DateObject();
        private String id;
        private Boolean global;
        private Integer timeSpent;
        private Assignee assignee = new Assignee();
        private Integer priority;
        private Boolean jiraIdRemoved;
        private Boolean read;
        private String productPlatform;
        private Integer filesAttached;
        private String parentId;
        private Integer type;
        private String productId;
        private String createdBy;
        private Integer status;
        private String productAppGroup;
        private String reporterId;
        private String jiraId;
        private String projectId;
        private Boolean removed;
        private String assigneeLastName;
        private String description;
        private String clientId;
        private String jiraSprintName;
        private String projectName;
        private String customFollowUpType;

        //getters & setters
    }

    class D_DateObject {

        private String javaClass;
        private Integer time;

        //getters & setters
    }

    class Assignee {

        private String javaClass;
        private Boolean isSem;
        private Integer subType;
        private Boolean doNotCampaign;
        private Integer status;
        private Integer rating;
        private String firstName;
        private SalesProcess salesProcess = new SalesProcess();
        private String lastName;
        private Boolean doNotEmail;
        private Boolean doNotCall;
        private Integer stage;
        private Boolean isRemoved;

        //getters & setters
    }

    class SalesProcess {

        private Boolean editable;
        private String javaClass;
        private Rules rules;

        //getters & setters
    }

    class Rules {

        private AList list = new AList();
        private String javaClass;
        //getters & setters
    }

    class AList {
    }

It doesn't seem as though gson is setting any of the variables in any of the classes. Am I doing something wrong? The JSON is being retrieved from a different location and I do not have access to change it. Thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情废物 2024-12-08 03:53:03

我尝试了你的示例代码并发现了 2 个错误。

  1. 您的 main 方法需要是静态的,您可以抛出异常或捕获它
  2. D_DateObject 中指定的时间值需要是 float (或 double)类型而不是 Integer

So

public static void main(String[] args) throws FileNotFoundException {
    BufferedReader json = new BufferedReader(new FileReader("jsonResults.txt"));
    Gson gson = new Gson();
    MyContainer container = gson.fromJson(json, MyContainer.class);
}

然后

class D_DateObject {

 private String javaClass;
 private float time; // this needed to be changed

 //getters & setters
}

使用您的对象解析对象(使用 gson 1.7.1 )

I tried your example code and noticed 2 errors.

  1. Your main method needs to be static and you could throw an exception or catch it
  2. the time value specified in D_DateObject needs to be of type float (or double) not Integer

So

public static void main(String[] args) throws FileNotFoundException {
    BufferedReader json = new BufferedReader(new FileReader("jsonResults.txt"));
    Gson gson = new Gson();
    MyContainer container = gson.fromJson(json, MyContainer.class);
}

and

class D_DateObject {

 private String javaClass;
 private float time; // this needed to be changed

 //getters & setters
}

Then the object was parsed using your objects (using gson 1.7.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文