Gson反序列化问题

时间:2011-08-25 12:43:27

标签: gson deserialization

我需要帮助使用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,我无权更改它。提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

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

  1. 您的主要方法需要是静态的,您可以抛出异常或抓住它
  2. D_DateObject中指定的时间值必须是float(或double)类型而不是Integer
  3. 所以

    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)