org.json.JSONObject.optJSONArray()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(14.0k)|赞(0)|评价(0)|浏览(429)

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

JSONObject.optJSONArray介绍

[英]Returns the value mapped by name if it exists and is a JSONArray. Returns null otherwise.
[中]返回按名称映射的值(如果该值存在且为JSONArray)。否则返回null。

代码示例

代码示例来源:origin: facebook/facebook-android-sdk

public ParameterComponent(final JSONObject component) throws JSONException {
    name = component.getString(PARAMETER_NAME_KEY);
    value = component.optString(PARAMETER_VALUE_KEY);

    ArrayList<PathComponent> pathComponents = new ArrayList<>();
    JSONArray jsonPathArray = component.optJSONArray(PARAMETER_PATH_KEY);
    if (null != jsonPathArray) {
      for (int i = 0; i < jsonPathArray.length(); i++) {
        PathComponent pathComponent = new PathComponent((jsonPathArray.getJSONObject(i)));
        pathComponents.add(pathComponent);
      }
    }
    path = pathComponents;
    pathType = component.optString(Constants.EVENT_MAPPING_PATH_TYPE_KEY,
        Constants.PATH_TYPE_ABSOLUTE);

  }
}

代码示例来源:origin: facebook/facebook-android-sdk

public static List<String> getCategories(Place place) {
  JSONObject jsonData = place.getJson();
  List<String> categories = new ArrayList<>();
  JSONArray jsonCategories = jsonData.optJSONArray(Place.CATEGORY_LIST);
  if (jsonCategories != null) {
    int length = jsonCategories.length();
    for (int i=0; i<length; i++) {
      JSONObject jsonCategory = jsonCategories.optJSONObject(i);
      if (jsonCategory != null) {
        String category = jsonCategory.optString("name");
        categories.add(category);
      }
    }
  }
  return categories;
}

代码示例来源:origin: stackoverflow.com

JSONObject jsonResponse = new JSONObject(theresultstring);
JSONArray jsonMainNode = jsonResponse.optJSONArray("data");//this is the main array header key name which i didn't posted in array by this I'm able to get into my array
JSONObject jsonChildNode = jsonMainNode.getJSONObject(0);            
String number = jsonChildNode.optString("id");
String name = jsonChildNode.optString("number");
String resume = jsonChildNode.optString("mobile");
String address = jsonChildNode.optString("name");

代码示例来源:origin: facebook/facebook-android-sdk

private synchronized static JSONObject parseAppGateKeepersFromJSON(
    final String applicationId,
    JSONObject gateKeepersJSON) {
  JSONObject result;
  if (fetchedAppGateKeepers.containsKey(applicationId)) {
    result = fetchedAppGateKeepers.get(applicationId);
  } else {
    result = new JSONObject();
  }
  JSONArray arr = gateKeepersJSON.optJSONArray(APPLICATION_GRAPH_DATA);
  JSONObject gateKeepers = null;
  if (arr != null) {
    gateKeepers = arr.optJSONObject(0);
  }
  // If there does exist a valid JSON object in arr, initialize result with this JSON object
  if (gateKeepers != null && gateKeepers.optJSONArray(APPLICATION_GATEKEEPER_FIELD) != null) {
    JSONArray data = gateKeepers.optJSONArray(APPLICATION_GATEKEEPER_FIELD);
    for (int i = 0; i < data.length(); i++) {
      try {
        JSONObject gk = data.getJSONObject(i);
        result.put(gk.getString("key"), gk.getBoolean("value"));
      } catch (JSONException je) {
        Utility.logd(Utility.LOG_TAG, je);
      }
    }
  }
  fetchedAppGateKeepers.put(applicationId, result);
  return result;
}

代码示例来源:origin: facebook/facebook-android-sdk

private static Map<Integer, Set<Integer>> parseJSONDefinition(JSONObject definition) {
  JSONArray itemsArray = definition.optJSONArray("items");
  if (itemsArray.length() == 0) {
    return null;
  }
  Map<Integer, Set<Integer>> items = new HashMap<>();
  for (int i = 0; i < itemsArray.length(); i++) {
    JSONObject item = itemsArray.optJSONObject(i);
    if (item == null) {
      continue;
    }
    int code = item.optInt("code");
    if (code == 0) {
      continue;
    }
    Set<Integer> subcodes = null;
    JSONArray subcodesArray = item.optJSONArray("subcodes");
    if (subcodesArray != null && subcodesArray.length() > 0) {
      subcodes = new HashSet<>();
      for (int j = 0; j < subcodesArray.length(); j++) {
        int subCode = subcodesArray.optInt(j);
        if (subCode != 0) {
          subcodes.add(subCode);
        }
      }
    }
    items.put(code, subcodes);
  }
  return items;
}

代码示例来源:origin: xmuSistone/AndroidPileLayout

/**
 * 从asset读取文件json数据
 */
private void initDataList() {
  dataList = new ArrayList<>();
  try {
    InputStream in = getAssets().open("preset.config");
    int size = in.available();
    byte[] buffer = new byte[size];
    in.read(buffer);
    String jsonStr = new String(buffer, "UTF-8");
    JSONObject jsonObject = new JSONObject(jsonStr);
    JSONArray jsonArray = jsonObject.optJSONArray("result");
    if (null != jsonArray) {
      int len = jsonArray.length();
      for (int j = 0; j < 3; j++) {
        for (int i = 0; i < len; i++) {
          JSONObject itemJsonObject = jsonArray.getJSONObject(i);
          ItemEntity itemEntity = new ItemEntity(itemJsonObject);
          dataList.add(itemEntity);
        }
      }
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: facebook/facebook-android-sdk

public List<AppLink> getAppLinks() {
  List<AppLink> appLinks = new ArrayList<>();
  JSONObject appLinkJson = jsonData.optJSONObject(APP_LINKS);
  if (appLinkJson != null) {
    JSONArray appArray = appLinkJson.optJSONArray("android");
    if (appArray != null) {
      int length = appArray.length();
      for (int i = 0; i < length; i++) {
        JSONObject linkJson = appArray.optJSONObject(i);
        if (linkJson != null) {
          String appName = linkJson.optString("app_name");
          Intent intent = new Intent(Intent.ACTION_VIEW);
          String packageName = linkJson.optString("package");
          String className = linkJson.optString("class");
          if (!TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(className)) {
            intent.setClassName(packageName, className);
          }
          String url = linkJson.optString("url");
          if (url != null) {
            intent.setData(Uri.parse(url));
          }
          appLinks.add(new AppLink(appName, intent));
        }
      }
    }
  }
  return appLinks;
}

代码示例来源:origin: facebook/facebook-android-sdk

JSONArray jsonPathArray = mapping.getJSONArray("path");
List<PathComponent> path = new ArrayList<>();
for (int i = 0; i < jsonPathArray.length(); i++) {
  JSONObject jsonPath = jsonPathArray.getJSONObject(i);
  PathComponent component = new PathComponent(jsonPath);
  path.add(component);
JSONArray jsonParameterArray = mapping.optJSONArray("parameters");
List<ParameterComponent> parameters = new ArrayList<>();
if (null != jsonParameterArray) {
  for (int i = 0; i < jsonParameterArray.length(); i++) {
    JSONObject jsonParameter = jsonParameterArray.getJSONObject(i);
    ParameterComponent component = new ParameterComponent(jsonParameter);
    parameters.add(component);

代码示例来源:origin: alibaba/Tangram-Android

@Override
public void parseWith(JSONObject data) {
  super.parseWith(data);
  if (data != null) {
    JSONArray jsonCols = data.optJSONArray(KEY_COLS);
    if (jsonCols != null) {
      cols = new float[jsonCols.length()];
      for (int i = 0; i < cols.length; i++) {
        cols[i] = (float) jsonCols.optDouble(i, 0);
      }
    } else {
      cols = new float[0];
    }
    JSONArray jsonRows = data.optJSONArray(KEY_ROWS);
    if (jsonRows != null) {
      rows = new float[jsonRows.length()];
      for (int i = 0; i < rows.length; i++) {
        rows[i] = (float) jsonRows.optDouble(i, 0);
      }
    } else {
      rows = new float[0];
    }
  }
}

代码示例来源:origin: facebook/facebook-android-sdk

JSONObject jsonObject = response.getJSONObject();
if (jsonObject != null) {
  JSONArray jsonArray = jsonObject.optJSONArray(PARAM_DATA);
  if (jsonArray != null) {
    int length = jsonArray.length();
    places = new ArrayList<>(length);
    for (int i = 0; i < length; i++) {
      places.add(new Place(jsonArray.getJSONObject(i)));

代码示例来源:origin: alibaba/Tangram-Android

@Override
public void parseWith(JSONObject data) {
  super.parseWith(data);
  if (data != null) {
    JSONArray layouts = data.optJSONArray(KEY_MIXED_LAYOUTS);
    if (layouts != null && layouts.length() > 0) {
      for (int i = 0, length = layouts.length(); i < length; i++) {
        JSONObject l = layouts.optJSONObject(i);
        if (l != null) {
          String type = l.optString(MVResolver.KEY_TYPE);
          if (TextUtils.isEmpty(type)) continue;
          CardInfo info = new CardInfo();
          info.type = type;
          info.itemCount = l.optInt("count", 0);
          info.data = l;
          cardInfos.add(info);
        }
      }
    }
  }
}

代码示例来源:origin: jeremylong/DependencyCheck

advisory.setCwe(object.optString("cwe", null));
final JSONArray findings = object.optJSONArray("findings");
for (int i = 0; i < findings.length(); i++) {
  final JSONObject finding = findings.getJSONObject(i);
  final String version = finding.optString("version", null);
  final JSONArray paths = finding.optJSONArray("paths");
  for (int j = 0; j < paths.length(); j++) {
    final String path = paths.getString(j);
    if (path != null && path.equals(advisory.getModuleName())) {
final JSONArray jsonCves = object.optJSONArray("cves");
final List<String> stringCves = new ArrayList<>();
if (jsonCves != null) {
  for (int j = 0; j < jsonCves.length(); j++) {
    stringCves.add(jsonCves.getString(j));

代码示例来源:origin: facebook/facebook-android-sdk

return;
JSONArray permissionsArray = result.optJSONArray("data");
if (permissionsArray == null) {
  return;
for (int i = 0; i < permissionsArray.length(); i++) {
  JSONObject permissionEntry = permissionsArray.optJSONObject(i);
  if (permissionEntry == null) {

代码示例来源:origin: facebook/facebook-android-sdk

JSONObject listObject = listResponse.getJSONObject();
assertNotNull(listObject);
JSONArray jsonList = listObject.optJSONArray("data");
assertNotNull(jsonList);
for (int i = 0; i < jsonList.length(); i++) {
  JSONObject imageObject = jsonList.getJSONObject(i);
  if (imageId.get().equals(imageObject.optString("id"))) {
    found = true;

代码示例来源:origin: facebook/facebook-android-sdk

private static Map<String, Map<String, FetchedAppSettings.DialogFeatureConfig>> parseDialogConfigurations(
    JSONObject dialogConfigResponse) {
  HashMap<String, Map<String, FetchedAppSettings.DialogFeatureConfig>> dialogConfigMap
      = new HashMap<>();
  if (dialogConfigResponse != null) {
    JSONArray dialogConfigData = dialogConfigResponse.optJSONArray("data");
    if (dialogConfigData != null) {
      for (int i = 0; i < dialogConfigData.length(); i++) {
        FetchedAppSettings.DialogFeatureConfig dialogConfig =
            FetchedAppSettings.DialogFeatureConfig.parseDialogConfig(
                dialogConfigData.optJSONObject(i));
        if (dialogConfig == null) {
          continue;
        }
        String dialogName = dialogConfig.getDialogName();
        Map<String, FetchedAppSettings.DialogFeatureConfig> featureMap =
            dialogConfigMap.get(dialogName);
        if (featureMap == null) {
          featureMap = new HashMap<>();
          dialogConfigMap.put(dialogName, featureMap);
        }
        featureMap.put(dialogConfig.getFeatureName(), dialogConfig);
      }
    }
  }
  return dialogConfigMap;
}

代码示例来源:origin: DV8FromTheWorld/JDA

final JSONArray emotesArray = guildJson.getJSONArray("emojis");
final JSONArray voiceStateArray = guildJson.getJSONArray("voice_states");
final JSONArray featuresArray = guildJson.optJSONArray("features");
final JSONArray presencesArray = guildJson.optJSONArray("presences");
final long ownerId = Helpers.optLong(guildJson, "owner_id", 0L);
final long afkChannelId = Helpers.optLong(guildJson, "afk_channel_id", 0L);
for (int i = 0; i < roleArray.length(); i++)
  JSONObject obj = roleArray.getJSONObject(i);
  Role role = createRole(guildObj, obj, guildId);
  guildObj.getRolesMap().put(role.getIdLong(), role);
  LOG.warn("Finished setup for guild with a null owner. GuildId: {} OwnerId: {}", guildId, guildJson.opt("owner_id"));
for (int i = 0; i < channelArray.length(); i++)
  JSONObject channelJson = channelArray.getJSONObject(i);
  createGuildChannel(guildObj, channelJson);
    .setSystemChannel(guildObj.getTextChannelById(systemChannelId));
for (int i = 0; i < presencesArray.length(); i++)
  JSONObject presence = presencesArray.getJSONObject(i);
  final long userId = presence.getJSONObject("user").getLong("id");
  MemberImpl member = (MemberImpl) guildObj.getMembersMap().get(userId);

代码示例来源:origin: alibaba/Tangram-Android

@Override
  public void parseWith(JSONObject data) {
    super.parseWith(data);
    if (data != null) {
      column = data.optInt(KEY_COLUMN, 0);
      autoExpand = data.optBoolean(KEY_AUTO_EXPAND, false);
      JSONArray jsonCols = data.optJSONArray(KEY_COLS);
      if (jsonCols != null) {
        cols = new float[jsonCols.length()];
        for (int i = 0; i < cols.length; i++) {
          cols[i] = (float) jsonCols.optDouble(i, 0);
        }
      } else {
        cols = new float[0];
      }
      hGap = Style.parseSize(data.optString(KEY_H_GAP), 0);
      vGap = Style.parseSize(data.optString(KEY_V_GAP), 0);
    }
  }
}

代码示例来源:origin: aws-amplify/aws-sdk-android

assertEquals(DefaultEventClient.class.getName(), className);
JSONArray observers = obj.optJSONArray("observers");
assertNotNull(observers);
assertEquals(1, observers.length());
JSONArray globalAttributes = obj.optJSONArray("globalAttributes");
assertNotNull(globalAttributes);
assertEquals(2, globalAttributes.length());
assertTrue(globalAttributes.getJSONObject(0).has("attr1")
    || globalAttributes.getJSONObject(0).has("attr2"));
if (globalAttributes.getJSONObject(0).has("attr1")) {
JSONArray globalMetrics = obj.optJSONArray("globalMetrics");
assertNotNull(globalMetrics);
assertEquals(2, globalMetrics.length());
assertEquals(2, eventTypeAttributes.length());
assertTrue(eventTypeAttributes.has("event_type_1"));
JSONArray eventType1Object = eventTypeAttributes.optJSONArray("event_type_1");
JSONArray eventType2Object = eventTypeAttributes.optJSONArray("event_type_2");
assertEquals(2, eventTypeMetrics.length());
assertTrue(eventTypeMetrics.has("event_type_1"));
JSONArray eventType1MetricObject = eventTypeMetrics.optJSONArray("event_type_1");
JSONArray eventType2MetricObject = eventTypeMetrics.optJSONArray("event_type_2");

代码示例来源:origin: alibaba/Tangram-Android

JSONArray componentArray = data.optJSONArray(KEY_ITEMS);
if (componentArray != null && isParseCell) {
  final int cellLength = Math.min(componentArray.length(), maxChildren);
  for (int i = 0; i < cellLength; i++) {
    final JSONObject cellData = componentArray.optJSONObject(i);

代码示例来源:origin: aws-amplify/aws-sdk-android

assertNotNull(uniqueId);
JSONArray globalAttributes = obj.optJSONArray("globalAttributes");
assertNotNull(globalAttributes);
assertEquals(2, globalAttributes.length());
assertTrue(globalAttributes.getJSONObject(0).has("attr1")
          || globalAttributes.getJSONObject(0).has("attr2"));
if (globalAttributes.getJSONObject(0).has("attr1")) {
JSONArray globalMetrics = obj.optJSONArray("globalMetrics");
assertNotNull(globalMetrics);
assertEquals(2, globalMetrics.length());
assertTrue(eventTypeAttributes.has("event_type_1"));
JSONArray eventType1Object = eventTypeAttributes
                   .optJSONArray("event_type_1");
assertEquals(1, eventType1Object.length());
                   .optJSONArray("event_type_2");
assertTrue(eventTypeMetrics.has("event_type_1"));
JSONArray eventType1MetricObject = eventTypeMetrics
                      .optJSONArray("event_type_1");
                      .optJSONArray("event_type_2");

相关文章

JSONObject类方法