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

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

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

JSONObject.has介绍

[英]Returns true if this object has a mapping for name. The mapping may be #NULL.
[中]如果此对象具有名称映射,则返回true。映射可能为#NULL。

代码示例

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

for (int i = 0; i < innerProjectarray.length(); i++) {

  JSONObject obj = innerProjectarray.getJSONObject(i);
  if (obj.has("PROJECT_NUMBER")) {
    String projectnumber1 = obj.getString("PROJECT_NUMBER");
  }

  if (obj.has("PROJECT_NAME")) {
    String projectname1 = obj.getString("PROJECT_NAME");
  }
}

代码示例来源:origin: loklak/loklak_server

public int getRequestFrequency(String path) {
  if (!this.json.has("frequency")) return -1;
  JSONObject paths = this.json.getJSONObject("frequency");
  if (!paths.has(path)) return -1;
  return paths.getInt(path);
}

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

private void transformCardCellData(JSONObject cardData) {
  try {
    if (cardData.has("iconList")) {
      cardData.put(Card.KEY_ITEMS, cardData.getJSONArray("iconList"));
    } else if (cardData.has("centerBannerList")) {
      cardData.put(Card.KEY_ITEMS, cardData.getJSONArray("centerBannerList"));
    }
  } catch (JSONException e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: RipMeApp/ripme

private List<JSONObject> getSubAlbumJSONs(JSONObject data) {
    List<JSONObject> subalbumJSONs = new ArrayList<>();
    if (data.has("subAlbums")) {
      JSONArray subalbums = data.getJSONArray("subAlbums");
      for (int idx = 0; idx < subalbums.length(); idx++) {
        JSONObject subalbumJSON = subalbums.getJSONObject(idx);
        subalbumJSONs.add(subalbumJSON);
      }
    }
    return subalbumJSONs;
  }
}

代码示例来源:origin: loklak/loklak_server

public void setPermission(String servletCanonicalName, String key, String value){
  if(!permissions.has(servletCanonicalName)) permissions.put(servletCanonicalName, new JSONObject());
  permissions.getJSONObject(servletCanonicalName).put(key, value);
}

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

JSONObject appLinkArgsJson = new JSONObject(jsonString);
String version = appLinkArgsJson.getString(APPLINK_VERSION_KEY);
  appLinkData.arguments = appLinkArgsJson.getJSONObject(APPLINK_METHOD_ARGS_KEY);
  if (appLinkData.arguments.has(METHOD_ARGS_REF_KEY)) {
    appLinkData.ref = appLinkData.arguments.getString(METHOD_ARGS_REF_KEY);
  } else if (appLinkData.arguments.has(ARGUMENTS_REFERER_DATA_KEY)) {
    JSONObject refererData = appLinkData.arguments.getJSONObject(
        ARGUMENTS_REFERER_DATA_KEY);
    if (refererData.has(REFERER_DATA_REF_KEY)) {
      appLinkData.ref = refererData.getString(REFERER_DATA_REF_KEY);
  if (appLinkData.arguments.has(METHOD_ARGS_TARGET_URL_KEY)) {
    appLinkData.targetUri = Uri.parse(
        appLinkData.arguments.getString(METHOD_ARGS_TARGET_URL_KEY));
  if (appLinkData.arguments.has(ARGUMENTS_EXTRAS_KEY)) {
    JSONObject extrasData =
        appLinkData.arguments.getJSONObject(ARGUMENTS_EXTRAS_KEY);
    if (extrasData.has(EXTRAS_DEEPLINK_CONTEXT_KEY)) {
      JSONObject deeplink_context =
          extrasData.getJSONObject(EXTRAS_DEEPLINK_CONTEXT_KEY);
      if (deeplink_context.has(PROMOTION_CODE_KEY)) {
        appLinkData.promotionCode =
            deeplink_context.getString(PROMOTION_CODE_KEY);

代码示例来源:origin: lzyzsd/JsBridge

public static Message toObject(String jsonStr) {
  Message m =  new Message();
  try {
    JSONObject jsonObject = new JSONObject(jsonStr);
    m.setHandlerName(jsonObject.has(HANDLER_NAME_STR) ? jsonObject.getString(HANDLER_NAME_STR):null);
    m.setCallbackId(jsonObject.has(CALLBACK_ID_STR) ? jsonObject.getString(CALLBACK_ID_STR):null);
    m.setResponseData(jsonObject.has(RESPONSE_DATA_STR) ? jsonObject.getString(RESPONSE_DATA_STR):null);
    m.setResponseId(jsonObject.has(RESPONSE_ID_STR) ? jsonObject.getString(RESPONSE_ID_STR):null);
    m.setData(jsonObject.has(DATA_STR) ? jsonObject.getString(DATA_STR):null);
    return m;
  } catch (JSONException e) {
    e.printStackTrace();
  }
  return m;
}

代码示例来源:origin: loklak/loklak_server

/**
 * If during thinking we observe something that we want to memorize, we can memorize this here
 * @param featureName the object key
 * @param observation the object value
 * @return the thought
 */
public SusiThought addObservation(String featureName, String observation) {
  JSONArray data = getData();
  for (int i = 0; i < data.length(); i++) {
    JSONObject spark = data.getJSONObject(i);
    if (!spark.has(featureName)) {
      spark.put(featureName, observation);
      return this;
    }
  }
  data.put(new JSONObject().put(featureName, observation));
  return this;
}

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

private Post jsonToPost(JSONObject post) throws JSONException {
  String message = null;
  if (post.has("message")) {
    message = post.getString("message");
  }
  String picture = null;
  if (post.has("picture")) {
    picture = post.getString("picture");
  }
  String created_time = post.getString("created_time");
  String id = post.getString("id");
  JSONObject from = post.getJSONObject("from");
  String from_name = from.getString("name");
  String from_id = from.getString("id");
  return new Post(message, created_time, id, picture, from_name, from_id);
}

代码示例来源:origin: loklak/loklak_server

/**
 * All details of the creation of this thought is collected in a metadata statement.
 * @return the set of meta information for this thought
 */
private JSONObject getMetadata() {
  JSONObject md;
  if (this.has(metadata_name)) md = this.getJSONObject(metadata_name); else {
    md = new JSONObject();
    this.put(metadata_name, md);
  }
  if (!md.has("count")) md.put("count", getData().length());
  return md;
}

代码示例来源:origin: facebook/stetho

private void handleRemoteMessage(JsonRpcPeer peer, String message)
  throws IOException, MessageHandlingException, JSONException {
 // Parse as a generic JSONObject first since we don't know if this is a request or response.
 JSONObject messageNode = new JSONObject(message);
 if (messageNode.has("method")) {
  handleRemoteRequest(peer, messageNode);
 } else if (messageNode.has("result")) {
  handleRemoteResponse(peer, messageNode);
 } else {
  throw new MessageHandlingException("Improper JSON-RPC message: " + message);
 }
}

代码示例来源:origin: loklak/loklak_server

public static TwitterTimeline peers(final String protocolhostportstub, final String query, final TwitterTimeline.Order order, final String source, final int count, final int timezoneOffset, final String provider_hash) throws IOException {
  TwitterTimeline tl = new TwitterTimeline(order);
  String urlstring = "";
  urlstring = protocolhostportstub + "/api/peers.json";
  byte[] response = ClientConnection.download(urlstring);
  if (response == null || response.length == 0) return tl;
  JSONObject json = new JSONObject(new String(response, StandardCharsets.UTF_8));
  JSONArray statuses = json.has("statuses") ? json.getJSONArray("statuses") : null;
  if (statuses != null) {
    for (Object tweet_obj: statuses) {
      JSONObject tweet = (JSONObject) tweet_obj;
      JSONObject user = tweet.has("user") ? (JSONObject) tweet.remove("user") : null;
      if (user == null) continue;
      tweet.put("provider_type", (Object) ProviderType.REMOTE.name());
      tweet.put("provider_hash", provider_hash);
      UserEntry u = new UserEntry(user);
      TwitterTweet t = new TwitterTweet(tweet);
      tl.add(t, u);
    }
  }
  //System.out.println(parser.text());
  return tl;
}

代码示例来源:origin: vdurmont/emoji-java

protected static Emoji buildEmojiFromJSON(
 JSONObject json
) throws UnsupportedEncodingException {
 if (!json.has("emoji")) {
  return null;
 }
 byte[] bytes = json.getString("emoji").getBytes("UTF-8");
 String description = null;
 if (json.has("description")) {
  description = json.getString("description");
 }
 boolean supportsFitzpatrick = false;
 if (json.has("supports_fitzpatrick")) {
  supportsFitzpatrick = json.getBoolean("supports_fitzpatrick");
 }
 List<String> aliases = jsonArrayToStringList(json.getJSONArray("aliases"));
 List<String> tags = jsonArrayToStringList(json.getJSONArray("tags"));
 return new Emoji(description, supportsFitzpatrick, aliases, tags, bytes);
}

代码示例来源:origin: apache/geode

public List<String> getArrayValues(String key) {
 List<String> result = new ArrayList<>();
 if (jsonObject.has(key)) {
  JSONArray jsonArray = jsonObject.getJSONArray(key);
  for (int i = 0; i < jsonArray.length(); i++) {
   result.add(jsonArray.getString(i));
  }
 }
 return result;
}

代码示例来源:origin: loklak/loklak_server

for(int i = 0;i < blogs.length(); i++) {
  JSONObject blog = (JSONObject)blogs.get(i);
  assertTrue(blog.has("blog_url"));
  assertTrue(blog.has("title"));
  assertTrue(blog.has("posted_on"));
  assertTrue(blog.has("content"));
  assertTrue(blog.has("author"));
  assertThat(blog.getString("author"), is(author));

代码示例来源:origin: loklak/loklak_server

/**
 * create a clone of a json object as a SusiThought object
 * @param json the 'other' thought, probably an exported and re-imported thought
 */
public SusiThought(JSONObject json) {
  this();
  if (json.has(this.metadata_name)) this.put(this.metadata_name, json.getJSONObject(this.metadata_name));
  if (json.has(this.data_name)) this.setData(json.getJSONArray(this.data_name));
  if (json.has("actions")) this.put("actions", json.getJSONArray("actions"));
}

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

public static CurrentPlaceResult fromJson(JSONObject json) throws JSONException {

    CurrentPlaceResult response = new CurrentPlaceResult();

    if (json.has(PARAM_DATA)) {
      JSONArray array = json.getJSONArray(PARAM_DATA);
      int length = array.length();
      response.places = new ArrayList<>(length);
      for (int i=0; i<length; i++) {
        JSONObject placeJson = array.getJSONObject(i);
        response.places.add(new Place(placeJson));
      }
    }
    if (json.has(PARAM_SUMMARY)) {
      JSONObject summaryJson = json.getJSONObject(PARAM_SUMMARY);
      response.tracking = summaryJson.getString(PARAM_TRACKING);
    }

    return response;
  }
}

代码示例来源:origin: loklak/loklak_server

public void setPermission(String servletCanonicalName, String key, long value){
  if(!permissions.has(servletCanonicalName)) permissions.put(servletCanonicalName, new JSONObject());
  permissions.getJSONObject(servletCanonicalName).put(key, value);
}

代码示例来源:origin: RipMeApp/ripme

@Override
  public void rip() throws IOException {
    LOGGER.info("Retrieving " + this.url);
    String html = Http.url(url).get().html();
    String jsonString = html.substring(html.indexOf("var flashVars = {d: '") + 21);
    jsonString = jsonString.substring(0, jsonString.indexOf("'"));
    JSONObject json    = new JSONObject(new String(Base64.decode(jsonString)));
    JSONObject jsonURL = new JSONObject(new String(Base64.decode(json.getString("url"))));
    String encryptedURL = jsonURL.getJSONObject("u").getString("l");
    String vidURL = "";
    for (char c : encryptedURL.toCharArray()) {
      if (decryptDict.has(Character.toString(c))) {
        vidURL += decryptDict.getString(Character.toString(c));
      }
      else {
        vidURL += c;
      }
    }
    addURLToDownload(new URL(vidURL), HOST + "_" + getGID(this.url));
    waitForThreads();
  }
}

代码示例来源:origin: facebook/stetho

private static JSONObject formatHeadersAsJSON(InspectorHeaders headers) {
 JSONObject json = new JSONObject();
 for (int i = 0; i < headers.headerCount(); i++) {
  String name = headers.headerName(i);
  String value = headers.headerValue(i);
  try {
   if (json.has(name)) {
    // Multiple headers are separated with a new line.
    json.put(name, json.getString(name) + "\n" + value);
   } else {
    json.put(name, value);
   }
  } catch (JSONException e) {
   throw new RuntimeException(e);
  }
 }
 return json;
}

相关文章

JSONObject类方法