如何使用Google的GSON解析JSON无效标识符?

时间:2011-11-26 23:22:35

标签: java json gson

我有一个返回JSON的REST Web服务。作为一个例子,我有以下回应:

  

{"oslc_cm:results":[{"dc:identifier":7,"calm:relatedTestCase":[],"calm:relatedTestScript":[],"rtc_cm:com.ibm.team.build.linktype.includedDeployments.com.ibm.team.build.common.link.includedInDeployment":[],"calm:relatedTestPlan":[],"rtc_cm:com.ibm.team.enterprise.deployment.linktype.deploymentDefinition.packageDefinition":[],"rtc_cm:com.ibm.team.workitem.linktype.attachment.attachment":[],"rtc_cm:state":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/workflows\/_X1GzsPC9EeCek69P-ztT9w\/states\/com.ibm.team.workitem.taskWorkflow\/1"},"rtc_cm:com.ibm.team.enterprise.packaging.linktype.resultWorkItem.result":[],"calm:tracksChanges":[],"rtc_cm:contextId":"_X1GzsPC9EeCek69P-ztT9w","rtc_cm:com.ibm.team.workitem.linktype.copiedworkitem.copies":[],"rtc_cm:com.ibm.team.workitem.linktype.resolvesworkitem.resolvedBy":[],"rtc_cm:com.ibm.team.enterprise.promotion.linktype.resultWorkItem.promoted":[],"rtc_cm:com.ibm.team.enterprise.package.linktype.packageDefinition.packageDefinition":[],"dc:creator":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/users\/_hhbosPC1EeCsJbI2ZgPDog"},"rtc_cm:com.ibm.team.workitem.linktype.copiedworkitem.copiedFrom":[],"rtc_cm:modifiedBy":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/users\/_hhbosPC1EeCsJbI2ZgPDog"},"rtc_cm:ownedBy":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/users\/_hhbosPC1EeCsJbI2ZgPDog"},"oslc_cm:severity":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/enumerations\/_X1GzsPC9EeCek69P-ztT9w\/severity\/severity.literal.l3"},"rtc_cm:com.ibm.team.enterprise.deployment.linktype.deploymentBuildResult.packageBuildResult":[],"calm:relatedExecutionRecord":[],"rdf:resource":"https:\/\/localhost:9443\/ccm\/resource\/itemName\/com.ibm.team.workitem.WorkItem\/7","calm:affectsExecutionResult":[],"rtc_cm:projectArea":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/projectareas\/_X1GzsPC9EeCek69P-ztT9w"},"rtc_cm:com.ibm.team.enterprise.packaging.linktype.resultWorkItem.promoted":[],"rtc_cm:com.ibm.team.workitem.linktype.schedulePredecessor.predecessor":[],"rtc_cm:correctedEstimate":null,"rtc_cm:teamArea":null,"rtc_cm:com.ibm.team.enterprise.promotion.linktype.promotionBuildResult.promotionBuildResult":[],"rtc_cm:com.ibm.team.enterprise.promotion.linktype.promotedChangeSets.promotedChangeSets":[],"dc:created":"2011-10-07T08:36:36.166Z","rtc_cm:resolution":null,"rtc_cm:archived":false,"rtc_cm:com.ibm.team.workitem.linktype.blocksworkitem.dependsOn":[],"calm:affectsPlanItem":[],"dc:subject":"","rtc_cm:resolvedBy":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/users\/_YNh4MOlsEdq4xpiOKg5hvA"},"calm:relatedTestSuite":[],"rtc_cm:com.ibm.team.workitem.linktype.parentworkitem.parent":[],"rtc_cm:filedAgainst":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/resource\/itemOid\/com.ibm.team.workitem.Category\/_ZQ3nEvC9EeCek69P-ztT9w"},"rtc_cm:com.ibm.team.workitem.linktype.relatedartifact.relatedArtifact":[],"dc:type":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/types\/_X1GzsPC9EeCek69P-ztT9w\/task"},"rtc_cm:com.ibm.team.enterprise.promotion.linktype.promotedBuildMaps.promotedBuildMaps":[],"dc:modified":"2011-10-07T08:51:52.006Z","rtc_cm:estimate":null,"rtc_cm:comments":[],"rtc_cm:plannedFor":null,"rtc_cm:com.ibm.team.workitem.linktype.textualReference.textuallyReferenced":[],"rtc_cm:com.ibm.team.connector.ccbridge.common.ver2wi.s":[],"rtc_cm:com.ibm.team.workitem.linktype.duplicateworkitem.duplicateOf":[],"rtc_cm:foundIn":null,"calm:tracksRequirement":[],"calm:implementsRequirement":[],"rtc_cm:com.ibm.team.workitem.linktype.resolvesworkitem.resolves":[],"rtc_cm:com.ibm.team.scm.svn.linkType.workItem.s":[],"rtc_cm:com.ibm.team.enterprise.promotion.linktype.resultWorkItem.result":[],"rtc_cm:com.ibm.team.workitem.linktype.relatedworkitem.related":[],"rtc_cm:timeSheet":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/workitems\/_ZadWAPC_EeC8XKWU4bgPxg\/rtc_cm:timeSheet"},"rtc_cm:com.ibm.team.workitem.linktype.blocksworkitem.blocks":[],"rtc_cm:com.ibm.team.build.linktype.reportedWorkItems.com.ibm.team.build.common.link.reportedAgainstBuilds":[],"rtc_cm:com.ibm.team.workitem.linktype.parentworkitem.children":[],"rtc_cm:com.ibm.team.enterprise.promotion.linktype.promotionDefinition.promotionDefinition":[],"rtc_cm:due":null,"rtc_cm:com.ibm.team.workitem.linktype.schedulePredecessor.successor":[],"rtc_cm:com.ibm.team.filesystem.workitems.change_set.com.ibm.team.scm.ChangeSet":[],"calm:relatedRequirement":[],"rtc_cm:timeSpent":null,"rtc_cm:resolved":null,"rtc_cm:startDate":null,"dc:title":"Test1","rtc_cm:progressTracking":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/workitems\/_ZadWAPC_EeC8XKWU4bgPxg\/progressTracking"},"rtc_cm:com.ibm.team.build.linktype.includedWorkItems.com.ibm.team.build.common.link.includedInBuilds":[],"calm:elaboratedByArchitectureElement":[],"oslc_cm:priority":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/enumerations\/_X1GzsPC9EeCek69P-ztT9w\/priority\/priority.literal.l01"},"rtc_cm:subscribers":[{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/users\/_hhbosPC1EeCsJbI2ZgPDog"}],"rtc_cm:com.ibm.team.workitem.linktype.duplicateworkitem.duplicates":[],"calm:testedByTestCase":[],"rtc_cm:com.ibm.team.build.linktype.includedPackages.com.ibm.team.build.common.link.includedInPackages":[],"dc:description":"","calm:blocksTestExecutionRecord":[],"rtc_cm:com.ibm.team.enterprise.package.linktype.packageBuildResult.packageBuildResult":[],"calm:affectedByDefect":[],"rtc_cm:com.ibm.team.connector.ccbridge.common.act2wi.s":[],"oslc_cm:relatedChangeManagement":[]},{"dc:identifier":14,"calm:relatedTestCase":[],"calm:relatedTestScript":[],"rtc_cm:com.ibm.team.build.linktype.includedDeployments.com.ibm.team.build.common.link.includedInDeployment":[],"calm:relatedTestPlan":[],"rtc_cm:com.ibm.team.enterprise.deployment.linktype.deploymentDefinition.packageDefinition":[],"rtc_cm:com.ibm.team.workitem.linktype.attachment.attachment":[],"rtc_cm:state":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/workflows\/_X1GzsPC9EeCek69P-ztT9w\/states\/com.ibm.team.workitem.defectWorkflow\/1"},"rtc_cm:com.ibm.team.enterprise.packaging.linktype.resultWorkItem.result":[],"calm:tracksChanges":[],"rtc_cm:contextId":"_X1GzsPC9EeCek69P-ztT9w","rtc_cm:com.ibm.team.workitem.linktype.copiedworkitem.copies":[],"rtc_cm:com.ibm.team.workitem.linktype.resolvesworkitem.resolvedBy":[],"rtc_cm:com.ibm.team.enterprise.promotion.linktype.resultWorkItem.promoted":[],"rtc_cm:com.ibm.team.enterprise.package.linktype.packageDefinition.packageDefinition":[],"dc:creator":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/users\/_hhbosPC1EeCsJbI2ZgPDog"},"rtc_cm:com.ibm.team.workitem.linktype.copiedworkitem.copiedFrom":[],"rtc_cm:modifiedBy":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/users\/_hhbosPC1EeCsJbI2ZgPDog"},"rtc_cm:ownedBy":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/users\/_hhbosPC1EeCsJbI2ZgPDog"},"oslc_cm:severity":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/enumerations\/_X1GzsPC9EeCek69P-ztT9w\/severity\/severity.literal.l3"},"rtc_cm:com.ibm.team.enterprise.deployment.linktype.deploymentBuildResult.packageBuildResult":[],"calm:relatedExecutionRecord":[],"rdf:resource":"https:\/\/localhost:9443\/ccm\/resource\/itemName\/com.ibm.team.workitem.WorkItem\/14","calm:affectsExecutionResult":[],"rtc_cm:projectArea":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/projectareas\/_X1GzsPC9EeCek69P-ztT9w"},"rtc_cm:com.ibm.team.enterprise.packaging.linktype.resultWorkItem.promoted":[],"rtc_cm:com.ibm.team.workitem.linktype.schedulePredecessor.predecessor":[],"rtc_cm:correctedEstimate":null,"rtc_cm:teamArea":null,"rtc_cm:com.ibm.team.enterprise.promotion.linktype.promotionBuildResult.promotionBuildResult":[],"rtc_cm:com.ibm.team.enterprise.promotion.linktype.promotedChangeSets.promotedChangeSets":[],"dc:created":"2011-10-14T12:02:30.283Z","rtc_cm:resolution":null,"rtc_cm:archived":false,"rtc_cm:com.ibm.team.workitem.linktype.blocksworkitem.dependsOn":[],"calm:affectsPlanItem":[],"dc:subject":"","rtc_cm:resolvedBy":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/users\/_YNh4MOlsEdq4xpiOKg5hvA"},"calm:relatedTestSuite":[],"rtc_cm:com.ibm.team.workitem.linktype.parentworkitem.parent":[],"rtc_cm:filedAgainst":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/resource\/itemOid\/com.ibm.team.workitem.Category\/_ZQ3nEvC9EeCek69P-ztT9w"},"rtc_cm:com.ibm.team.workitem.linktype.relatedartifact.relatedArtifact":[],"dc:type":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/types\/_X1GzsPC9EeCek69P-ztT9w\/defect"},"rtc_cm:com.ibm.team.enterprise.promotion.linktype.promotedBuildMaps.promotedBuildMaps":[],"dc:modified":"2011-10-14T12:03:43.215Z","rtc_cm:estimate":null,"rtc_cm:comments":[],"rtc_cm:plannedFor":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/iterations\/_YD4PgPC9EeCek69P-ztT9w"},"rtc_cm:com.ibm.team.workitem.linktype.textualReference.textuallyReferenced":[],"rtc_cm:com.ibm.team.connector.ccbridge.common.ver2wi.s":[],"rtc_cm:com.ibm.team.workitem.linktype.duplicateworkitem.duplicateOf":[],"rtc_cm:foundIn":null,"calm:tracksRequirement":[],"calm:implementsRequirement":[],"rtc_cm:com.ibm.team.workitem.linktype.resolvesworkitem.resolves":[],"rtc_cm:com.ibm.team.scm.svn.linkType.workItem.s":[],"rtc_cm:com.ibm.team.enterprise.promotion.linktype.resultWorkItem.result":[],"rtc_cm:com.ibm.team.workitem.linktype.relatedworkitem.related":[],"rtc_cm:timeSheet":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/workitems\/_Vo0mcPZcEeCfT6LO8_tPoQ\/rtc_cm:timeSheet"},"rtc_cm:com.ibm.team.workitem.linktype.blocksworkitem.blocks":[],"rtc_cm:com.ibm.team.build.linktype.reportedWorkItems.com.ibm.team.build.common.link.reportedAgainstBuilds":[],"rtc_cm:com.ibm.team.workitem.linktype.parentworkitem.children":[],"rtc_cm:com.ibm.team.enterprise.promotion.linktype.promotionDefinition.promotionDefinition":[],"rtc_cm:due":null,"rtc_cm:com.ibm.team.workitem.linktype.schedulePredecessor.successor":[],"rtc_cm:com.ibm.team.filesystem.workitems.change_set.com.ibm.team.scm.ChangeSet":[],"calm:relatedRequirement":[],"rtc_cm:timeSpent":null,"rtc_cm:resolved":null,"rtc_cm:startDate":null,"dc:title":"TestDefect1","rtc_cm:progressTracking":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/workitems\/_Vo0mcPZcEeCfT6LO8_tPoQ\/progressTracking"},"rtc_cm:com.ibm.team.build.linktype.includedWorkItems.com.ibm.team.build.common.link.includedInBuilds":[],"calm:elaboratedByArchitectureElement":[],"oslc_cm:priority":{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/enumerations\/_X1GzsPC9EeCek69P-ztT9w\/priority\/priority.literal.l07"},"rtc_cm:subscribers":[{"rdf:resource":"https:\/\/localhost:9443\/ccm\/oslc\/users\/_hhbosPC1EeCsJbI2ZgPDog"}],"rtc_cm:com.ibm.team.workitem.linktype.duplicateworkitem.duplicates":[],"calm:testedByTestCase":[],"rtc_cm:com.ibm.team.build.linktype.includedPackages.com.ibm.team.build.common.link.includedInPackages":[],"dc:description":"","calm:blocksTestExecutionRecord":[],"rtc_cm:com.ibm.team.enterprise.package.linktype.packageBuildResult.packageBuildResult":[],"calm:affectedByDefect":[],"rtc_cm:com.ibm.team.connector.ccbridge.common.act2wi.s":[],"oslc_cm:relatedChangeManagement":[]},],"oslc_cm:totalCount":2}

现在我想使用GSON将其转换为POJO对象。对于顶级根节点,我创建了以下类:

public class JSONContainer {

    ArrayList<JSONWorkItem> items = new ArrayList<JSONWorkItem>();

    public ArrayList<JSONWorkItem> getItems() {
        return items;
    }

}

这不起作用,因为我认为ArrayList应该是名称oslc_cm:results,但我不能,因为这不是一个有效的Java标识符。那我该怎么办?

1 个答案:

答案 0 :(得分:1)

答案可在Gson User Guide中找到。您可以为此目的使用注释SerializedName。

所以POJO类看起来应该是这样的:

public class JSONContainer {

    @SerializedName("oslc_cm:results")
    ArrayList<JSONWorkItem> items = new ArrayList<JSONWorkItem>();

    public ArrayList<JSONWorkItem> getItems() {
        return items;
    }

}