com.atlassian.jira.util.json.JSONArray类的使用及代码示例

x33g5p2x  于2022-01-22 转载在 其他  
字(11.4k)|赞(0)|评价(0)|浏览(139)

本文整理了Java中com.atlassian.jira.util.json.JSONArray类的一些代码示例,展示了JSONArray类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JSONArray类的具体详情如下:
包路径:com.atlassian.jira.util.json.JSONArray
类名称:JSONArray

JSONArray介绍

[英]A JSONArray is an ordered sequence of values. Its external text form is a string wrapped in square brackets with commas separating the values. The internal form is an object having get and opt methods for accessing the values by index, and put methods for adding or replacing values. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, String, or the JSONObject.NULL object.

The constructor can convert a JSON text into a Java object. The toString method converts to JSON text.

A get method returns a value if one can be found, and throws an exception if one cannot be found. An opt method returns a default value instead of throwing an exception, and so is useful for obtaining optional values.

The generic get() and opt() methods return an object which you can cast or query for type. There are also typed get and opt methods that do type checking and type coersion for you.

The texts produced by the toString methods strictly conform to JSON syntax rules. The constructors are more forgiving in the texts they will accept:

  • An extra , (comma) may appear just before the closing bracket.
  • The null value will be inserted when there is , (comma) elision.
  • Strings may be quoted with ' (single quote).
  • Strings do not need to be quoted at all if they do not begin with a quote or single quote, and if they do not contain leading or trailing spaces, and if they do not contain any of these characters: { } [ ] / \ : , = ; # and if they do not look like numbers and if they are not the reserved words true, false, or null.
  • Values can be separated by ; (semicolon) as well as by , (comma).
  • Numbers may have the 0- (octal) or 0x- (hex) prefix.
  • Comments written in the slashshlash, slashstar, and hash conventions will be ignored.
    [中]JSONArray是一个有序的值序列。它的外部文本形式是一个用方括号括起来的字符串,值之间用逗号分隔。内部表单是一个对象,它具有getopt方法用于按索引访问值,以及put方法用于添加或替换值。这些值可以是以下任何类型:BooleanJSONArrayJSONObjectNumberStringJSONObject.NULL object
    构造函数可以将JSON文本转换为Java对象。toString方法将转换为JSON文本。
    如果可以找到get方法,则返回一个值;如果找不到,则抛出一个异常。opt方法返回默认值而不是引发异常,因此对于获取可选值非常有用。
    泛型get()opt()方法返回一个对象,您可以强制转换或查询该对象的类型。还有类型化的getopt方法可以为您进行类型检查和类型协同。
    toString方法生成的文本严格遵守JSON语法规则。构造器在他们将接受的文本中更加宽容:
    *右括号前可能会出现额外的,(逗号)。
    *当存在,(逗号)省略时,将插入null值。
    *字符串可以用'引用(单引号)。
    *如果字符串不以引号或单引号开头,如果字符串不包含前导空格或尾随空格,如果字符串不包含以下任何字符:[21$]],如果字符串看起来不像数字,如果字符串不是保留字truefalsenull,则根本不需要引用字符串。
    *值可以用;(分号)和,(逗号)分隔。
    *数字可以有0-(八进制)或0x-(十六进制)前缀。
    *以slashshlash、slashstar和哈希约定编写的注释将被忽略。

代码示例

代码示例来源:origin: com.atlassian.jira/jira-api

/**
 * Convert the passed array into SharePermissions.
 * 
 * @param array the JSON array to convert.
 * @return the converted SharePermission.
 * @throws JSONException if an error occurs during convertion.
 */
public static SharePermissions fromJsonArray(final JSONArray array) throws JSONException
{
  notNull("array", array);
  final Set<SharePermission> permissions = new HashSet<SharePermission>(array.length());
  for (int i = 0; i < array.length(); i++)
  {
    permissions.add(SharePermissionUtils.fromJsonObject(array.getJSONObject(i)));
  }
  return new SharePermissions(permissions);
}

代码示例来源:origin: com.atlassian.jira/jira-core

public String getJqlReservedWordsJson() throws JSONException
{
  final Set<String> reservedWords = jqlStringSupport.getJqlReservedWords();
  JSONArray results = new JSONArray();
  for (String reservedWord : reservedWords)
  {
    results.put(reservedWord);
  }
  return results.toString();
}

代码示例来源:origin: com.atlassian.jira/jira-api

/**
 * Produce a JSONArray containing the values of the members of this
 * JSONObject.
 *
 * @param names A JSONArray containing a list of key strings. This
 *              determines the sequence of the values in the result.
 * @return A JSONArray of values.
 * @throws JSONException If any of the values are non-finite numbers.
 */
public JSONArray toJSONArray(final JSONArray names) throws JSONException
{
  if ((names == null) || (names.length() == 0))
  {
    return null;
  }
  final JSONArray ja = new JSONArray();
  for (int i = 0; i < names.length(); i += 1)
  {
    ja.put(opt(names.getString(i)));
  }
  return ja;
}

代码示例来源:origin: com.atlassian.jira/jira-api

/**
 * Put a value in the JSONArray, where the value will be a
 * JSONArray which is produced from a Collection.
 *
 * @param value A Collection value.
 * @return this.
 */
public JSONArray put(final Collection<Object> value)
{
  put(new JSONArray(value));
  return this;
}

代码示例来源:origin: com.atlassian.jira/jira-api

private static Collection<String> getGroups(final JSONArray jsonArray) throws JSONException
{
  Set<String> groups = Sets.newHashSetWithExpectedSize(jsonArray.length());
  for (int i = 0 ; i<jsonArray.length(); i++)
  {
    groups.add(jsonArray.getString(i));
  }
  return groups;
}

代码示例来源:origin: com.atlassian.jira/jira-api

private static Collection<Long> getRoleIds(final JSONArray jsonArray) throws JSONException
{
  Set<Long> roleIds = Sets.newHashSetWithExpectedSize(jsonArray.length());
  for (int i = 0 ; i<jsonArray.length(); i++)
  {
    roleIds.add(jsonArray.getLong(i));
  }
  return roleIds;
}

代码示例来源:origin: com.atlassian.jira/jira-core

private Collection<RemoteProject> parseProjects(final String response, final ApplicationLink applicationLink)
      throws ResponseException, CredentialsRequiredException
  {
    try
    {
      JSONArray json = new JSONArray(response);
      int length = json.length();
      final Collection<RemoteProject> projects = new ArrayList<RemoteProject>(length);
      for (int i = 0; i < length; i++)
      {
        projects.add(RemoteProject.from(applicationLink, json.getJSONObject(i)));
      }
      return projects;
    }
    catch (JSONException ex)
    {
      throw new RuntimeException("Unable to parse the JSON response for get projects : " + response, ex);
    }
  }
}

代码示例来源:origin: com.atlassian.jira/jira-api

/**
 * Produce a JSONArray containing the names of the elements of this
 * JSONObject.
 *
 * @return A JSONArray containing the key strings, or null if the JSONObject
 *         is empty.
 */
public JSONArray names()
{
  final JSONArray ja = new JSONArray();
  for (final String key : keySet())
  {
    ja.put(key);
  }
  return ja.length() == 0 ? null : ja;
}

代码示例来源:origin: com.atlassian.jira/jira-core

private static List<String> parseShortcut(String shortcut) throws JSONException
{
  if (JSON_VALUE_ARRAY.matcher(shortcut).matches())
  {
    final JSONArray array = new JSONArray(shortcut);
    final List<String> result = Lists.newArrayListWithCapacity(array.length());
    for (int i = 0; i < array.length(); i++) {
      result.add(array.getString(i));
    }
    return result;
  }
  else if (JSON_VALUE_STRING.matcher(shortcut).matches())
  {
    final String key = "shortcut";
    JSONObject json = new JSONObject(String.format("{ \"%s\": %s }", key, shortcut));
    return Arrays.asList(json.getString(key));
  }
  else
  {
    final List<String> result = new ArrayList<String>();
    for (char c : shortcut.toCharArray())
    {
      result.add(String.valueOf(c));
    }
    return result;
  }
}

代码示例来源:origin: com.atlassian.jira.plugins/jira-healthcheck-plugin

@Override
public String failureReason()
{
  if (failureReasons.isEmpty())
  {
    return null;
  }
  if (failureReasons.size() == 1)
  {
    return failureReasons.get(0);
  }
  return new JSONArray(failureReasons).toString();
}

代码示例来源:origin: com.atlassian.jira/jira-api

/**
 * Produce a JSONObject by combining a JSONArray of names with the values
 * of this JSONArray.
 *
 * @param names A JSONArray containing a list of key strings. These will be
 *              paired with the values.
 * @return A JSONObject, or null if there are no names or if this JSONArray
 *         has no values.
 * @throws JSONException If any of the names are null.
 */
public JSONObject toJSONObject(final JSONArray names) throws JSONException
{
  if ((names == null) || (names.length() == 0) || (length() == 0))
  {
    return null;
  }
  final JSONObject jo = new JSONObject();
  for (int i = 0; i < names.length(); i += 1)
  {
    jo.put(names.getString(i), opt(i));
  }
  return jo;
}

代码示例来源:origin: com.atlassian.jira/jira-api

if (index < length())
  while (index != length())
    put(JSONObject.NULL);
  put(value);

代码示例来源:origin: com.marvelution.jira.plugins/jira-jenkins-plugin

final JSONArray actions = jsonBuild.optJSONArray("actions");
if (actions != null) {
  for (int index = 0; index < actions.length(); index++) {
    final JSONObject object = actions.optJSONObject(index);
    if (object == null) {
      continue;
      for (int ii = 0; ii < causes.length(); ii++) {
        final JSONObject cause = causes.getJSONObject(ii);
        if (cause.has("shortDescription")) {
if (artifacts != null && artifacts.length() > 0) {
  for (int index = 0; index < artifacts.length(); index++) {
    final JSONObject artifact = artifacts.getJSONObject(index);
    build.getArtifacts().add(new Artifact(artifact.getString("fileName"),
                       artifact.getString("displayPath"), artifact.getString("relativePath")));
if (culprits != null && culprits.length() > 0) {
  for (int index = 0; index < culprits.length(); index++) {
    final JSONObject culprit = culprits.getJSONObject(index);
    String id = optJsonString(culprit, "id");
    if (id == null && culprit.has("absoluteUrl")) {
  if (changeSet != null && changeSet.has("items")) {
    final JSONArray items = changeSet.optJSONArray("items");
    if (items != null && items.length() > 0) {
      for (int index = 0; index < items.length(); index++) {
        final JSONObject item = items.getJSONObject(index);
        String commitId = String.valueOf(index);

代码示例来源:origin: com.atlassian.jira/jira-api

/**
 * Get the optional object value associated with an index.
 *
 * @param index The index must be between 0 and length() - 1.
 * @return An object value, or null if there is no
 *         object at that index.
 */
public Object opt(final int index)
{
  return ((index < 0) || (index >= length())) ? null : list.get(index);
}

代码示例来源:origin: com.atlassian.jira/jira-api

/**
 * Put a key/value pair in the JSONObject, where the value will be a
 * JSONArray which is produced from a Collection.
 *
 * @param key   A key string.
 * @param values A Collection value.
 * @return this.
 * @throws JSONException If the value is non-finite number or if the key is null.
 */
public JSONObject put(final String key, final Collection<Object> values) throws JSONException
{
  put(key, new JSONArray(values));
  return this;
}

代码示例来源:origin: com.atlassian.jira/jira-api

/**
 * Append a boolean value. This increases the array's length by one.
 *
 * @param value A boolean value.
 * @return this.
 */
public JSONArray put(final boolean value)
{
  put(value ? Boolean.TRUE : Boolean.FALSE);
  return this;
}

代码示例来源:origin: com.atlassian.jira/jira-api

final int len = length();
    ((JSONArray) v).write(writer);

代码示例来源:origin: com.atlassian.jira/jira-core

for (int i = 0; i < array.length(); i++)
  final Object arrayElement = array.opt(i);
  if (arrayElement!=null)

代码示例来源:origin: com.atlassian.jirawallboard/atlassian-wallboard-plugin

static public WallboardPluginSettings loadSettings(PluginSettingsFactory pluginSettingsFactory, ApplicationUser user)
{
  WallboardPluginSettings settings = new WallboardPluginSettings(pluginSettingsFactory, user);
  Object val = pluginSettingsFactory.createGlobalSettings().get(WALLBOARD_KEY + mapNullToBlank(settings.userKey));
  if (val == null)
  {
    settings.isConfigured = false;
    return settings;
  }
  JSONObject jsonRepresentation;
  try
  {
    jsonRepresentation = new JSONObject((String) val);
    JSONArray rawDashboardIds = jsonRepresentation.getJSONArray("dashboardIds");
    settings.dashboardIds = new ArrayList<>(rawDashboardIds.length());
    for (int i = 0; i < rawDashboardIds.length(); i++)
    {
      settings.dashboardIds.add(i, (String) rawDashboardIds.get(i));
    }
    settings.setCyclePeriod(jsonRepresentation.getInt(WallboardServlet.CYCLE_PERIOD.getKey()));
    settings.setTransitionFx(jsonRepresentation.getString(WallboardServlet.TRANSITION_FX.getKey()));
    settings.setRandom(jsonRepresentation.getBoolean(WallboardServlet.RANDOM.getKey()));
  }
  catch (JSONException e)
  {
    settings.isConfigured = false;
  }
  return settings;
}

代码示例来源:origin: com.atlassian.jira/jira-api

/**
 * Make a prettyprinted JSON text of this JSONArray.
 * Warning: This method assumes that the data structure is acyclical.
 *
 * @param indentFactor The number of spaces to add to each level of
 *                     indentation.
 * @return a printable, displayable, transmittable
 *         representation of the object, beginning
 *         with <code>[</code>&nbsp;<small>(left bracket)</small> and ending
 *         with <code>]</code>&nbsp;<small>(right bracket)</small>.
 * @throws JSONException if JSON object fails to convert to String
 */
public String toString(final int indentFactor) throws JSONException
{
  return toString(indentFactor, 0);
}

相关文章