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

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

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

JSONObject.getOrDefault介绍

暂无

代码示例

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

public static String getJsonWithUpdatedResources(String jsonConf, Map<String, Double> resourceUpdates) {
  try {
    JSONParser parser = new JSONParser();
    Object obj = parser.parse(jsonConf);
    JSONObject jsonObject = (JSONObject) obj;
    Map<String, Double> componentResourceMap =
      (Map<String, Double>) jsonObject.getOrDefault(
        Config.TOPOLOGY_COMPONENT_RESOURCES_MAP, new HashMap<String, Double>()
      );
    for (Map.Entry<String, Double> resourceUpdateEntry : resourceUpdates.entrySet()) {
      if (NormalizedResources.RESOURCE_NAME_NORMALIZER.getResourceNameMapping().containsValue(resourceUpdateEntry.getKey())) {
        // if there will be legacy values they will be in the outer conf
        jsonObject.remove(getCorrespondingLegacyResourceName(resourceUpdateEntry.getKey()));
        componentResourceMap.remove(getCorrespondingLegacyResourceName(resourceUpdateEntry.getKey()));
      }
      componentResourceMap.put(resourceUpdateEntry.getKey(), resourceUpdateEntry.getValue());
    }
    jsonObject.put(Config.TOPOLOGY_COMPONENT_RESOURCES_MAP, componentResourceMap);
    return jsonObject.toJSONString();
  } catch (ParseException ex) {
    throw new RuntimeException("Failed to parse component resources with json: " + jsonConf);
  }
}

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

JSONObject root = (JSONObject)parser.parse(message);
String msgType = (String)root.getOrDefault("type", "");

代码示例来源:origin: com.rethinkdb/rethinkdb-driver

public static Response parseFrom(long token, ByteBuffer buf) {
  if (Response.logger.isDebugEnabled()) {
    Response.logger.debug(
        "JSON Recv: Token: {} {}", token, Util.bufferToString(buf));
  }
  JSONObject jsonResp = Util.toJSON(buf);
  ResponseType responseType = ResponseType.fromValue(
      ((Long) jsonResp.get("t")).intValue()
  );
  ArrayList<Long> responseNoteVals = (ArrayList<Long>) jsonResp
    .getOrDefault("n", new ArrayList());
  ArrayList<ResponseNote> responseNotes = responseNoteVals
    .stream()
      .map(Long::intValue)
      .map(ResponseNote::maybeFromValue)
      .filter(Optional::isPresent)
      .map(Optional::get)
      .collect(Collectors.toCollection(ArrayList::new));
  Builder res = new Builder(token, responseType);
  if(jsonResp.containsKey("e")){
    res.setErrorType(((Long)jsonResp.get("e")).intValue());
  }
  return res.setNotes(responseNotes)
      .setProfile((JSONArray) jsonResp.getOrDefault("p", null))
      .setBacktrace((JSONArray) jsonResp.getOrDefault("b", null))
      .setData((JSONArray) jsonResp.getOrDefault("r", new JSONArray()))
      .build();
}

相关文章