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

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

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

JSONObject.<init>介绍

[英]Allows creation of a JSONObject from a Map. After that, both the generated JSONObject and the Map can be modified independently.
[中]允许从映射创建JSONObject。之后,生成的JSONObject和映射都可以独立修改。

代码示例

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

@Override
public byte[] mapRecord(TridentTuple tuple) {
  JSONObject obj = new JSONObject();
  if (this.columnFields != null) {
    for (String field : this.columnFields) {
      obj.put(field, tuple.getValueByField(field));
    }
  }
  return obj.toJSONString().getBytes();
}

代码示例来源:origin: shopizer-ecommerce/shopizer

@SuppressWarnings("unchecked")
@Override
public String toJSONString() {
  JSONArray jsonArray = new JSONArray();
  for(String kw : keywords) {
    JSONObject data = new JSONObject();
    data.put("name", kw);
    data.put("value", kw);
    jsonArray.add(data);
  }
  return jsonArray.toJSONString();
}

代码示例来源:origin: Vedenin/useful-java-links

public static void main(String[] args) {
    // convert Java to json
    JSONObject root = new JSONObject();
    root.put("message", "Hi");
    JSONObject place = new JSONObject();
    place.put("name", "World!");
    root.put("place", place);
    String json = root.toJSONString();
    System.out.println(json);

    System.out.println();
    // convert json to Java
    JSONObject obj = (JSONObject) JSONValue.parse(json);
    String message = (String) obj.get("message");
    place = (JSONObject) obj.get("place");
    String name = (String) place.get("name");
    System.out.println(message + " " + name);
  }
}

代码示例来源:origin: rhuss/jolokia

J4pVersionResponse(J4pVersionRequest pRequest, JSONObject pResponse) {
  super(pRequest,pResponse);
  JSONObject value = (JSONObject) getValue();
  agentVersion = (String) value.get("agent");
  protocolVersion = (String) value.get("protocol");
  info = (JSONObject) value.get("info");
  if (info == null) {
    info = new JSONObject();
  }
}

代码示例来源:origin: pentaho/pentaho-kettle

public JSONArray storeRecentSearch( String recentSearch ) {
 JSONArray recentSearches = getRecentSearches();
 try {
  if ( recentSearch == null || recentSearches.contains( recentSearch ) ) {
   return recentSearches;
  }
  recentSearches.add( recentSearch );
  if ( recentSearches.size() > 5 ) {
   recentSearches.remove( 0 );
  }
  PropsUI props = PropsUI.getInstance();
  String jsonValue = props.getRecentSearches();
  JSONParser jsonParser = new JSONParser();
  JSONObject jsonObject = jsonValue != null ? (JSONObject) jsonParser.parse( jsonValue ) : new JSONObject();
  jsonObject.put( getLogin(), recentSearches );
  props.setRecentSearches( jsonObject.toJSONString() );
 } catch ( Exception e ) {
  e.printStackTrace();
 }
 return recentSearches;
}

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

@Override
public JSONObject joinMessages(Map<String, Tuple> streamMessageMap, MessageGetStrategy messageGetStrategy) {
 JSONObject message = new JSONObject();
 for (String key : streamMessageMap.keySet()) {
  Tuple tuple = streamMessageMap.get(key);
  JSONObject obj = (JSONObject) messageGetStrategy.get(tuple);
  message.putAll(obj);
 }
 List<Object> emptyKeys = new ArrayList<>();
 for(Object key : message.keySet()) {
  Object value = message.get(key);
  if(value == null || value.toString().length() == 0) {
   emptyKeys.add(key);
  }
 }
 for(Object o : emptyKeys) {
  message.remove(o);
 }
 message.put(getClass().getSimpleName().toLowerCase() + ".joiner.ts", "" + System.currentTimeMillis());
 return  message;
}

代码示例来源:origin: ethereum/ethereumj

JSONObject envJSON = (JSONObject) testCaseJSONObj.get("env");
JSONObject execJSON = (JSONObject) testCaseJSONObj.get("exec");
JSONObject preJSON = (JSONObject) testCaseJSONObj.get("pre");
JSONObject postJSON = new JSONObject();
if (testCaseJSONObj.containsKey("post")) {
  postJSON = (JSONObject) testCaseJSONObj.get("post");
JSONArray callCreates = new JSONArray();
if (testCaseJSONObj.containsKey("callcreates"))
  callCreates = (JSONArray) testCaseJSONObj.get("callcreates");
Object logsJSON = new JSONArray();
if (testCaseJSONObj.containsKey("logs"))
  logsJSON = testCaseJSONObj.get("logs");

代码示例来源:origin: pentaho/pentaho-kettle

@SuppressWarnings( "unchecked" )
private void outPutRow( Object[] rowData ) throws KettleStepException {
 // We can now output an object
 data.jg = new JSONObject();
 data.jg.put( data.realBlocName, data.ja );
 String value = data.jg.toJSONString();
 if ( data.outputValue && data.outputRowMeta != null ) {
  Object[] outputRowData = RowDataUtil.addValueData( rowData, data.inputRowMetaSize, value );
  incrementLinesOutput();
  putRow( data.outputRowMeta, outputRowData );
 }
 if ( data.writeToFile && !data.ja.isEmpty() ) {
  // Open a file
  if ( !openNewFile() ) {
   throw new KettleStepException( BaseMessages.getString(
    PKG, "JsonOutput.Error.OpenNewFile", buildFilename() ) );
  }
  // Write data to file
  try {
   data.writer.write( value );
  } catch ( Exception e ) {
   throw new KettleStepException( BaseMessages.getString( PKG, "JsonOutput.Error.Writing" ), e );
  }
  // Close file
  closeFile();
 }
 // Data are safe
 data.rowsAreSafe = true;
 data.ja = new JSONArray();
}

代码示例来源:origin: linkedin/indextank-engine

@SuppressWarnings("unchecked")
private void addResult(JSONArray ja, SearchResult result) {
  JSONObject document = new JSONObject();
  document.putAll(result.getFields());
  document.put("docid", result.getDocId());
  document.put("query_relevance_score", result.getScore());
  for(Entry<Integer, Double> entry: result.getVariables().entrySet()) {
    document.put("variable_" + entry.getKey(), entry.getValue());
  }
  for(Entry<String, String> entry: result.getCategories().entrySet()) {
    document.put("category_" + entry.getKey(), entry.getValue());
  }
  ja.add(document);
}

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

@SuppressWarnings({"unchecked"})
public JSONObject getJSONObject() {
 JSONObject errorMessage = new JSONObject();
 errorMessage.put(Constants.GUID, UUID.randomUUID().toString());
 errorMessage.put(Constants.SENSOR_TYPE, "error");
 if (sensorTypes.size() == 1) {
  errorMessage.put(ErrorFields.FAILED_SENSOR_TYPE.getName(), sensorTypes.iterator().next());
 } else {
  errorMessage
    .put(ErrorFields.FAILED_SENSOR_TYPE.getName(), new JSONArray().addAll(sensorTypes));
 }
 errorMessage.put(ErrorFields.ERROR_TYPE.getName(), errorType.getType());
 addMessageString(errorMessage);
   addStacktrace(errorMessage);
 addTimestamp(errorMessage);
 addHostname(errorMessage);
 addRawMessages(errorMessage);
 addErrorHash(errorMessage);
 return errorMessage;
}

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

private static JSONObject join(JSONObject left, JSONObject right) {
 JSONObject message = new JSONObject();
 message.putAll(left);
 message.putAll(right);
 List<Object> emptyKeys = new ArrayList<>();
 for(Object key : message.keySet()) {
  Object value = message.get(key);
  if(value == null || value.toString().length() == 0) {
   emptyKeys.add(key);
  }
 }
 for(Object o : emptyKeys) {
  message.remove(o);
 }
 return message;
}

代码示例来源:origin: GlowstoneMC/Glowstone

/**
 * Saves to the file.
 */
@SuppressWarnings("unchecked")
protected void save() {
  JSONArray array = new JSONArray();
  for (BaseEntry entry : entries) {
    JSONObject obj = new JSONObject();
    for (Entry<String, String> mapEntry : entry.write().entrySet()) {
      obj.put(mapEntry.getKey(), mapEntry.getValue());
    }
    array.add(obj);
  }
  try (Writer writer = new FileWriter(file)) {
    array.writeJSONString(writer);
  } catch (Exception ex) {
    GlowServer.logger.log(Level.SEVERE, "Error writing to: " + file, ex);
  }
}

代码示例来源:origin: alibaba/jstorm

public Number connect(Map conf, TopologyContext context) throws IOException, NoOutputException {
  JSONObject setupInfo = new JSONObject();
  setupInfo.put("pidDir", context.getPIDDir());
  setupInfo.put("conf", conf);
  setupInfo.put("context", context);
  writeMessage(setupInfo);
  return (Number) ((JSONObject) readMessage()).get("pid");
}

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

@Override
public byte[] mapRecord(Tuple tuple) {
  JSONObject obj = new JSONObject();
  if (this.columnFields != null) {
    for (String field : this.columnFields) {
      obj.put(field, tuple.getValueByField(field));
    }
  }
  return obj.toJSONString().getBytes();
}

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

private TupleBasedDocument createDocument(JSONObject message,
                     Tuple tuple,
                     String sensorType,
                     FieldNameConverter fieldNameConverter) {
 // transform the message fields to the source fields of the indexed document
 JSONObject source = new JSONObject();
 for(Object k : message.keySet()){
  copyField(k.toString(), message, source, fieldNameConverter);
 }
 // define the document id
 String guid = ConversionUtils.convert(source.get(Constants.GUID), String.class);
 if(guid == null) {
  LOG.warn("Missing '{}' field; document ID will be auto-generated.", Constants.GUID);
 }
 // define the document timestamp
 Long timestamp = null;
 Object value = source.get(TIMESTAMP.getName());
 if(value != null) {
  timestamp = Long.parseLong(value.toString());
 } else {
  LOG.warn("Missing '{}' field; timestamp will be set to system time.", TIMESTAMP.getName());
 }
 return new TupleBasedDocument(source, guid, sensorType, timestamp, tuple);
}

代码示例来源:origin: pentaho/pentaho-kettle

JSONObject jo = new JSONObject();
  jo.put( outputField.getElementName(), data.inputRowMeta.getBoolean( row, data.fieldIndexes[i] ) );
  break;
 case ValueMetaInterface.TYPE_INTEGER:
  jo.put( outputField.getElementName(), data.inputRowMeta.getInteger( row, data.fieldIndexes[i] ) );
  break;
 case ValueMetaInterface.TYPE_NUMBER:
  jo.put( outputField.getElementName(), data.inputRowMeta.getNumber( row, data.fieldIndexes[i] ) );
  break;
 case ValueMetaInterface.TYPE_BIGNUMBER:
  break;
data.ja.add( jo );

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

Match gm = grok.match(originalMessage);
gm.captures();
JSONObject message = new JSONObject();
message.putAll(gm.toMap());
 errors.put(originalMessage, rte);
} else {
 message.put("original_string", originalMessage);
 for (String timeField : timeFields) {
  String fieldValue = (String) message.get(timeField);
  if (fieldValue != null) {
   message.put(timeField, toEpoch(fieldValue));
  message.put(Constants.Fields.TIMESTAMP.getName(), formatTimestamp(message.get(timestampField)));

代码示例来源:origin: Exslims/MercuryTrade

private void createEmptyFile() {
  try {
    FileWriter fileWriter = new FileWriter(HISTORY_FILE);
    JSONObject root = new JSONObject();
    root.put("messages", new JSONArray());
    fileWriter.write(root.toJSONString());
    fileWriter.flush();
    fileWriter.close();
  } catch (IOException e) {
    logger.error("Error during creating history file: ", e);
  }
}

代码示例来源:origin: pentaho/pentaho-kettle

@SuppressWarnings( "unchecked" )
public String getDatabases() {
 JSONArray list = new JSONArray();
 for ( int i = 0; i < repositoriesMeta.nrDatabases(); i++ ) {
  JSONObject databaseJSON = new JSONObject();
  databaseJSON.put( "name", repositoriesMeta.getDatabase( i ).getName() );
  list.add( databaseJSON );
 }
 return list.toString();
}

代码示例来源:origin: pentaho/pentaho-kettle

JSONObject json = new JSONObject();
for ( Header header : headers ) {
 Object previousValue = json.get( header.getName() );
 if ( previousValue == null ) {
  json.put( header.getName(), header.getValue() );
 } else if ( previousValue instanceof List ) {
  List<String> list = (List<String>) previousValue;
  list.add( (String) previousValue );
  list.add( header.getValue() );
  json.put( header.getName(), list );

相关文章