开发者

Gson Deserializing issues

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",
              "accountPla开发者_如何学JAVAtform":"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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜