gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:39:37 27 4
gpt4 key购买 nike

我有一个返回 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 标识符。那我能做什么呢?

最佳答案

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

所以 POJO 类应该如下所示:

public class JSONContainer {

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

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

}

关于java - 如何使用 Google 的 GSON 解析 JSON 无效标识符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8282403/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com