简体   繁体   中英

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:

["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. 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 "";
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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