Gson反序列化问题
我需要使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我尝试了你的示例代码并发现了 2 个错误。
So
然后
使用您的对象解析对象(使用 gson 1.7.1 )
I tried your example code and noticed 2 errors.
So
and
Then the object was parsed using your objects (using gson 1.7.1)