简体   繁体   English

从 Java JSONArray 解析值

[英]Parsing a value from a Java JSONArray

I am parsing a field from a jira page using JSONArray class, but I am getting the parsed field in this format:我正在使用JSONArray class 解析来自 jira 页面的字段,但是我得到了这种格式的解析字段:

["com.atlassian.greenhopper.service.sprint.Sprint@4104b49f
   [
      id=539,
      rapidViewId=20,
      state=FUTURE,
      name=Spring - February 2019 - Dev,
      startDate=<null>,
      endDate=<null>,
      completeDate=<null>,
      sequence=539,
      goal=<null>
   ]
"]

I would like to extract the name from this JSONArray from this, but I don't know how to do it since it's not in JSON format.我想从此JSONArray中提取名称,但我不知道该怎么做,因为它不是JSON格式。 Any help would be appreciated.任何帮助,将不胜感激。

Thanks in advance.提前致谢。

My code:我的代码:

final IssueField field = jira.getField(ISSUE_SPRINT_ID);
if (field == null) {
    log.error("Cannot load Sprint Info for issue {}", jira.getKey());
    return "NA";
}
final JSONArray data = (JSONArray) field.getValue();
if (data == null) {
    log.error("Cannot load Sprint Info for issue {}", jira.getKey());
    return "NA";
}

Found this horrible hack Here , not a great one, but solves my problem:) 在这里找到了这个可怕的黑客,不是一个很好的黑客,但解决了我的问题:)

/*
 * Horrible hack :( - but no other options apparently
 * See https://answers.atlassian.com/questions/92681/how-to-get-sprints-using-greenhopper-api
*/
private static final String NAME_FIELD_ATTRIBUTE = "name=";
private String extractSprintName(JSONArray fieldValue) {
    try {
        String value = (String) fieldValue.get(0);
        value = value.substring(value.indexOf(NAME_FIELD_ATTRIBUTE));
        return value.substring(NAME_FIELD_ATTRIBUTE.length(), value.indexOf(','));
    } catch ( JSONException e) {
        return "";
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM