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

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

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

JSONObject.optJSONObject介绍

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

代码示例

代码示例来源:origin: google/physical-web

/**
 * Get extra JSONObject value.
 * @param key The key of the stored value.
 * @return The stored value or null if it doesn't exist in specified form.
 */
public JSONObject optExtraJSONObject(String key) {
 return mExtraData.optJSONObject(key);
}

代码示例来源:origin: google/physical-web

/**
 * Get extra JSONObject value.
 * @param key The key of the stored value.
 * @return The stored value or null if it doesn't exist in specified form.
 */
public JSONObject optExtraJSONObject(String key) {
 return mExtraData.optJSONObject(key);
}

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

public JSONObject getJson(String field) {
  return jsonData.optJSONObject(field);
}

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

public static JSONObject tryGetJSONObjectFromResponse(JSONObject response, String propertyKey) {
  return response != null ? response.optJSONObject(propertyKey) : null;
}

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

public JSONObject optJsonObjectParam(String key) {
  if (extras.has(key))
    return extras.optJSONObject(key);
  if (style != null && style.extras != null)
    return style.extras.optJSONObject(key);
  return null;
}

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

public JSONObject optJsonObjectParam(String key) {
  if (extras.has(key)) {
    return extras.optJSONObject(key);
  }
  if (style != null && style.extras != null) {
    return style.extras.optJSONObject(key);
  }
  return null;
}

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

public String getCoverPhotoUrl() {
  JSONObject coverPhotoJson = jsonData.optJSONObject("cover");
  if (coverPhotoJson != null) {
    return coverPhotoJson.optString("source");
  }
  return null;
}

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

protected Uri getPictureUriOfGraphObject(JSONObject graphObject) {
  String uri = null;
  Object o = graphObject.opt(PICTURE);
  if (o instanceof String) {
    uri = (String) o;
  } else if (o instanceof JSONObject) {
    JSONObject data = ((JSONObject) o).optJSONObject("data");
    uri = data != null ? data.optString("url") : null;
  }
  if (uri != null) {
    return Uri.parse(uri);
  }
  return null;
}

代码示例来源:origin: google/physical-web

/**
 * Populate a UrlDevice with data from a given JSON object.
 * @param jsonObject a serialized UrlDevice.
 * @return The UrlDevice represented by the serialized object.
 */
public static UrlDevice jsonDeserialize(JSONObject jsonObject) {
 return new Builder(jsonObject.getString(ID_KEY), jsonObject.getString(URL_KEY))
   .setExtra(jsonObject.optJSONObject(EXTRA_KEY))
   .build();
}

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

@Override
  protected void processSuccess(GraphResponse response) {
    JSONObject results = Utility.tryGetJSONObjectFromResponse(
        response.getJSONObject(),
        objectId);
    if (results != null) {
      // See if we can get the OG object Id out
      JSONObject ogObject = results.optJSONObject("og_object");
      if (ogObject != null) {
        verifiedObjectId = ogObject.optString("id");
      }
    }
  }
}

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

public LatLng getPosition() {
  JSONObject location = jsonData.optJSONObject(LOCATION);
  if (location != null) {
    if (location.has("latitude") && location.has("longitude")) {
      double latitude = location.optDouble("latitude");
      double longitude = location.optDouble("longitude");
      return new LatLng(latitude, longitude);
    }
  }
  return null;
}

代码示例来源: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: alibaba/Tangram-Android

@Override
public void parseWith(@NonNull JSONObject data, @NonNull MVHelper resolver) {
  maxChildren = 1;
  this.extras = data;
  this.stringType = data.optString(KEY_TYPE);
  id = data.optString(KEY_ID, id == null ? "" : id);
  loadMore = data.optInt(KEY_LOAD_TYPE, 0) == LOAD_TYPE_LOADMORE;
  //you should alway assign hasMore to indicate there's more data explicitly
  if (data.has(KEY_HAS_MORE)) {
    hasMore = data.optBoolean(KEY_HAS_MORE);
  } else {
    if (data.has(KEY_LOAD_TYPE)) {
      hasMore = data.optInt(KEY_LOAD_TYPE) == LOAD_TYPE_LOADMORE;
    }
  }
  load = data.optString(KEY_API_LOAD, null);
  loadParams = data.optJSONObject(KEY_API_LOAD_PARAMS);
  loaded = data.optBoolean(KEY_LOADED, false);
  createCell(this, resolver, extras, serviceManager, true);
  //do not need parse style, leave style empty
  this.extras.remove(KEY_STYLE);
  style = new Style();
}

代码示例来源:origin: google/physical-web

/**
  * Populate a PwsResult with data from a given JSON object.
  * @param jsonObject a serialized PwsResult.
  * @return The PwsResult represented by the serialized object.
  */
 public static PwsResult jsonDeserialize(JSONObject jsonObject) {
  return new Builder(jsonObject.getString(REQUESTURL_KEY), jsonObject.getString(SITEURL_KEY))
    .setExtra(jsonObject.optJSONObject(EXTRA_KEY))
    .setTitle(jsonObject.optString(TITLE_KEY))
    .setDescription(jsonObject.optString(DESCRIPTION_KEY))
    .setIconUrl(jsonObject.optString(ICONURL_KEY))
    .setGroupId(jsonObject.optString(GROUPID_KEY))
    .build();
 }
}

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

protected void parseCell(MVHelper resolver, BaseCell cell, JSONObject json) {
  if (json != null) {
    cell.extras = json;
    cell.id = json.optString(KEY_BIZ_ID);
    if (TextUtils.isEmpty(cell.id) && json.has(KEY_ID)) {
      cell.id = json.optString(KEY_ID);
    }
    cell.type = json.optInt(KEY_TYPE);
    cell.stringType = json.optString(KEY_TYPE);
    cell.typeKey = json.optString(KEY_TYPE_KEY);
    String reuseId = json.optString(KEY_TYPE_REUSEID);
    if (!TextUtils.isEmpty(reuseId)) {
      cell.typeKey = reuseId;
    }
    cell.position = json.optInt(KEY_POSITION, -1);
    parseBizParams(cell, json);
    cell.parseWith(json);
    cell.parseWith(json, resolver);
    JSONObject styleJson = json.optJSONObject(KEY_STYLE);
    parseStyle(cell, styleJson);
    parseBizParams(cell, styleJson);
  } else {
    cell.extras = new JSONObject();
  }
}

代码示例来源:origin: androidquery/androidquery

JSONObject dia = jo.optJSONObject("dialog");

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

private synchronized void populateTestAccounts(JSONArray testAccounts,
                        JSONObject userAccountsMap) {
  for (int i = 0; i < testAccounts.length(); ++i) {
    JSONObject testAccount = testAccounts.optJSONObject(i);
    JSONObject testUser = userAccountsMap.optJSONObject(testAccount.optString("id"));
    try {
      testAccount.put("name", testUser.optString("name"));
    } catch (JSONException e) {
      Log.e(LOG_TAG, "Could not set name", e);
    }
    storeTestAccount(testAccount);
  }
}

代码示例来源:origin: ankidroid/Anki-Android

@Override
public void onRemoveSearch(String searchName) {
  Timber.d("OnRemoveSelection using search named: %s", searchName);
  try {
    JSONObject savedFiltersObj = getCol().getConf().optJSONObject("savedFilters");
    if (savedFiltersObj != null && savedFiltersObj.has(searchName)) {
      savedFiltersObj.remove(searchName);
      getCol().getConf().put("savedFilters", savedFiltersObj);
      getCol().flush();
      if (savedFiltersObj.length() == 0) {
        mMySearchesItem.setVisible(false);
      }
    }
  } catch (JSONException e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: ankidroid/Anki-Android

@Override
public void onSelection(String searchName) {
  Timber.d("OnSelection using search named: %s", searchName);
  JSONObject savedFiltersObj = getCol().getConf().optJSONObject("savedFilters");
  Timber.d("SavedFilters are %s", savedFiltersObj.toString());
  if (savedFiltersObj != null) {
    mSearchTerms = savedFiltersObj.optString(searchName);
    Timber.d("OnSelection using search terms: %s", mSearchTerms);
    mSearchView.setQuery(mSearchTerms, false);
    mSearchItem.expandActionView();
    searchCards();
  }
}

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

@Override
protected void processSuccess(GraphResponse response) {
  JSONArray dataSet = Utility.tryGetJSONArrayFromResponse(
      response.getJSONObject(),
      "data");
  if (dataSet != null) {
    for (int i = 0; i < dataSet.length(); i++) {
      JSONObject data = dataSet.optJSONObject(i);
      if (data != null) {
        objectIsLiked = true;
        JSONObject appData = data.optJSONObject("application");
        AccessToken accessToken = AccessToken.getCurrentAccessToken();
        if (appData != null &&
            AccessToken.isCurrentAccessTokenActive() &&
            Utility.areObjectsEqual(
                accessToken.getApplicationId(),
                appData.optString("id"))) {
          unlikeToken = data.optString("id");
        }
      }
    }
  }
}

相关文章

JSONObject类方法