例如,仅使用Rally的Web服务v2.0获取JSON形式的字段“属性类型为JSON的环境”。
最佳答案
这是一个Java代码,它输出Defect的严重性值:
public class GetSeverityValues {
public static void main(String[] args) throws URISyntaxException, IOException {
String host = "https://rally1.rallydev.com";
String username = "user@co.com";
String password = "secret";
String projectRef = "/project/12352608219";
String workspaceRef = "/workspace/12352608129";
String applicationName = "RESTExampleFindSeverityValues";
RallyRestApi restApi = null;
try {
restApi = new RallyRestApi(
new URI(host),
username,
password);
restApi.setApplicationName(applicationName);
QueryRequest typeDefRequest = new QueryRequest("TypeDefinition");
typeDefRequest.setFetch(new Fetch("ObjectID","Attributes"));
typeDefRequest.setWorkspace(workspaceRef);
typeDefRequest.setQueryFilter(new QueryFilter("Name", "=", "Defect"));
QueryResponse typeDefQueryResponse = restApi.query(typeDefRequest);
JsonObject typeDefJsonObject = typeDefQueryResponse.getResults().get(0).getAsJsonObject();
System.out.println(typeDefJsonObject.get("_ref"));
System.out.println(typeDefJsonObject.get("Attributes"));
int numberOfAttributes = typeDefJsonObject.getAsJsonObject("Attributes").get("Count").getAsInt();
QueryRequest attributeRequest = new QueryRequest(typeDefJsonObject.getAsJsonObject("Attributes"));
attributeRequest.setFetch(new Fetch("AllowedValues","ElementName", "Name"));
QueryResponse attributesQueryResponse = restApi.query(attributeRequest);
for (int i=0; i<attributesQueryResponse.getResults().size();i++){
String fieldName = attributesQueryResponse.getResults().get(i).getAsJsonObject().get("Name").getAsString();
if (fieldName.equals("Severity")){
JsonObject allowedValuesJsonObject = attributesQueryResponse.getResults().get(i).getAsJsonObject();
int numberOfSeverityValues = allowedValuesJsonObject.getAsJsonObject("AllowedValues").get("Count").getAsInt();
//System.out.println(numberOfSeverityValues);
QueryRequest allowedValuesRequest = new QueryRequest(allowedValuesJsonObject.getAsJsonObject("AllowedValues"));
allowedValuesRequest.setFetch(new Fetch("StringValue"));
QueryResponse allowedValuesResponse = restApi.query(allowedValuesRequest);
for (int j = 0; j < numberOfSeverityValues; j++){
JsonObject allowedAttributeValuesJsonObject = allowedValuesResponse.getResults().get(j).getAsJsonObject();
System.out.println(allowedAttributeValuesJsonObject.get("StringValue"));
}
}
}
}
finally{
if (restApi != null) {
restApi.close();
}
}
}
}