本文整理汇总了Java中com.ibm.json.java.JSONObject.put方法的典型用法代码示例。如果您正苦于以下问题:Java JSONObject.put方法的具体用法?Java JSONObject.put怎么用?Java JSONObject.put使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.ibm.json.java.JSONObject
的用法示例。
在下文中一共展示了JSONObject.put方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: process
import com.ibm.json.java.JSONObject; //导入方法依赖的package包/类
@Override
protected void process() throws Exception {
final StreamingOutput<OutputTuple> out = getOutput(0);
JSONObject json = new JSONObject();
for (int i = 0; i < 100; i++) {
json.put("username", "Frank");
json.put("tweet", "This JSON message also rocks: " + i);
json.put("timestamp", new Long(1048298240L + i));
/* Now submit tuple */
OutputTuple tuple = out.newTuple();
String jsonString = json.serialize();
long timeStamp = System.currentTimeMillis();
tuple.setString(0, jsonString);
tuple.setLong(1, timeStamp);
out.submit(tuple);
if (i != 0 && i % 20 == 0)
out.punctuate(Punctuation.WINDOW_MARKER);
}
// Make the set of tuples a window.
out.punctuate(Punctuation.WINDOW_MARKER);
}
示例2: buildContent
import com.ibm.json.java.JSONObject; //导入方法依赖的package包/类
private JSONObject buildContent(String text)
{
JSONObject contentItem = new JSONObject();
contentItem.put("userid", UUID.randomUUID().toString());
contentItem.put("id", UUID.randomUUID().toString());
contentItem.put("sourceid", "freetext");
contentItem.put("contenttype", "text/plain");
contentItem.put("language", "en");
contentItem.put("content", text);
JSONObject content = new JSONObject();
JSONArray contentItems = new JSONArray();
content.put("contentItems", contentItems);
contentItems.add(contentItem);
return content;
}
示例3: addToJson
import com.ibm.json.java.JSONObject; //导入方法依赖的package包/类
/**
* Helper method to add the device info to an existing JSON Object
*
* @param payload an existing JSON Object
* @return JSON Object with the device information added
*/
protected JSONObject addToJson(JSONObject payload) {
// this is to ensure we do not overwrite the device descriptor when we are updating a document
if (payload.get(JSON_DEVICE_DESCRIPTOR) == null) {
payload.put(JSON_DEVICE_DESCRIPTOR, mDeviceDescriptor);
}
if (mName != null) {
payload.put(JSON_NAME, mName);
}
if (mRegistrationType != null) {
payload.put(JSON_REGISTRATION_TYPE, mRegistrationType);
}
if (mData != null) {
payload.put(JSON_DATA, mData);
}
if (mUnencryptedData != null) {
payload.put(JSON_UNENCRYPTED_DATA, mUnencryptedData);
}
payload.put(JSON_REGISTERED, mRegistered);
payload.put(JSON_BLACKLIST, mBlacklisted);
return payload;
}
示例4: buildBeaconPayload
import com.ibm.json.java.JSONObject; //导入方法依赖的package包/类
private JSONObject buildBeaconPayload(Collection<Beacon> beacons) {
long detectedTime = System.currentTimeMillis();
JSONObject payload = new JSONObject();
JSONArray beaconArray = new JSONArray();
// build payload with nearest beacon only
Beacon nearestBeacon = beacons.iterator().next();
for (Beacon b : beacons) {
if (b.getDistance() < nearestBeacon.getDistance()) {
nearestBeacon = b;
}
}
PIBeaconData data = new PIBeaconData(nearestBeacon);
data.setDetectedTime(detectedTime);
data.setDeviceDescriptor(mDeviceDescriptor);
beaconArray.add(data.getBeaconAsJson());
payload.put("bnm", beaconArray);
return payload;
}
示例5: getBeaconAsJson
import com.ibm.json.java.JSONObject; //导入方法依赖的package包/类
/**
* Helper method to provide the class as a JSON Object
*
* @return JSON Object of the beacon's data
*/
public JSONObject getBeaconAsJson() {
try {
beaconValidator(this);
} catch (Exception e) {
Log.e("ERROR", e.toString());
e.printStackTrace();
}
JSONObject returnObj = new JSONObject();
JSONObject beaconData = new JSONObject();
beaconData.put("proximityUUID", proximityUUID);
beaconData.put("major", major);
beaconData.put("minor", minor);
beaconData.put("accuracy", accuracy);
beaconData.put("rssi", rssi);
beaconData.put("proximity", proximity);
returnObj.put("data", beaconData);
returnObj.put("descriptor", deviceDescriptor);
returnObj.put("detectedTime", detectedTime);
return returnObj;
}
示例6: testDeserializeArray
import com.ibm.json.java.JSONObject; //导入方法依赖的package包/类
/**
* Test that if the serialized value is
* an array, it ends up wrapped in an object.
*/
@Test
public void testDeserializeArray() throws Exception {
final String data = "[ 100, 500, false, 200, 400 ]";
final Topology t = new Topology();
TStream<String> array = t.strings(data);
TStream<JSONObject> json = JSONStreams.deserialize(array);
TStream<String> jsonString = JSONStreams.serialize(json);
JSONArray ja = (JSONArray) JSON.parse(data);
JSONObject jo = new JSONObject();
jo.put("payload", ja);
checkJsonOutput(jo, jsonString);
}
示例7: toTuple
import com.ibm.json.java.JSONObject; //导入方法依赖的package包/类
public static OutputTuple toTuple(DeviceCmd cmd, OutputTuple tuple) {
tuple.setString(Schemas.TYPE_ID, cmd.getDevice().getTypeId());
tuple.setString(Schemas.DEVICE_ID, cmd.getDevice().getId());
tuple.setString(Schemas.CMD_ID, cmd.getCmdId());
JSONObject payload = new JSONObject();
payload.put("d", cmd.getData()); //$NON-NLS-1$
Instant ts = cmd.getTs();
if (ts == null)
ts = Instant.now();
payload.put("ts", ts.toString()); //$NON-NLS-1$
try {
tuple.setString(Schemas.JSON_STRING, payload.serialize());
} catch (Exception e) {
throw new RuntimeException(e);
}
return tuple;
}
示例8: generateStatuses
import com.ibm.json.java.JSONObject; //导入方法依赖的package包/类
public static JSONObject[] generateStatuses(int count) {
Random r = new Random();
JSONObject[] statuses = new JSONObject[count];
for (int i = 0 ; i < count ; i++) {
JSONObject status = new JSONObject();
statuses[i] = status;
status.put("py_type", "T" + r.nextInt(4));
status.put("py_device", "D" + r.nextInt(10));
JSONObject payload = new JSONObject();
status.put("py_payload", payload);
payload.put("s1", (long) r.nextInt(1000));
payload.put("s2", "S2_" + r.nextInt(5000));
}
return statuses;
}
示例9: generateEvents
import com.ibm.json.java.JSONObject; //导入方法依赖的package包/类
public static JSONObject[] generateEvents(int count) {
Random r = new Random();
JSONObject[] events = new JSONObject[count];
for (int i = 0 ; i < count ; i++) {
JSONObject event = new JSONObject();
events[i] = event;
event.put("py_type", "T" + r.nextInt(4));
event.put("py_device", "D" + r.nextInt(10));
event.put("py_event", "E" + r.nextInt(5));
if (r.nextFloat() > 0.1) {
int offset = r.nextInt(20000) - 10000;
event.put("py_ts", System.currentTimeMillis() + offset);
}
JSONObject payload = new JSONObject();
event.put("py_data", payload);
payload.put("v1", (long) r.nextInt(1000));
payload.put("v2", "V2_" + r.nextInt(5000));
}
return events;
}
示例10: generateCommands
import com.ibm.json.java.JSONObject; //导入方法依赖的package包/类
public static JSONObject[] generateCommands(int count) {
Random r = new Random();
JSONObject[] commands = new JSONObject[count];
for (int i = 0 ; i < count ; i++) {
JSONObject cmd = new JSONObject();
commands[i] = cmd;
cmd.put("py_type", "T" + r.nextInt(4));
cmd.put("py_device", "D" + r.nextInt(10));
cmd.put("py_command", "C" + r.nextInt(5));
if (r.nextFloat() > 0.1) {
int offset = r.nextInt(20000) - 10000;
cmd.put("py_ts", System.currentTimeMillis() + offset);
}
JSONObject data = new JSONObject();
cmd.put("py_data", data);
data.put("c1", (long) r.nextInt(1000));
data.put("c2", "C2_" + r.nextInt(5000));
}
return commands;
}
示例11: testModifyingJson
import com.ibm.json.java.JSONObject; //导入方法依赖的package包/类
@Test
public void testModifyingJson() throws Exception {
final Topology t = new Topology();
final JSONObject value = new JSONObject();
value.put("question",QUESTION);
TStream<JSONObject> s = t.constants(Collections.singletonList(value));
TStream<String> jsonString = JSONStreams.serialize(s);
TStream<JSONObject> json = JSONStreams.deserialize(jsonString);
TStream<JSONObject> jsonm = modifyJSON(json);
assertFalse(value.containsKey("answer"));
JSONObject ev = new JSONObject();
ev.put("question", QUESTION);
ev.put("answer", 42l);
checkJsonOutput(ev, JSONStreams.serialize(jsonm));
}
示例12: testJSONAble
import com.ibm.json.java.JSONObject; //导入方法依赖的package包/类
@Test
public void testJSONAble() throws IOException, Exception {
Topology topology = new Topology();
final TestJSONAble value = new TestJSONAble(42,QUESTION);
TStream<TestJSONAble> s = topology.constants(
Collections.singletonList(value)).asType(TestJSONAble.class);
TStream<JSONObject> js = JSONStreams.toJSON(s);
JSONObject ev = new JSONObject();
ev.put("b", QUESTION);
ev.put("a", 42l);
checkJsonOutput(ev, JSONStreams.serialize(js));
}
示例13: testFlatten
import com.ibm.json.java.JSONObject; //导入方法依赖的package包/类
@Test
public void testFlatten() throws Exception {
final Topology t = new Topology();
final JSONObject value = new JSONObject();
final JSONArray array = new JSONArray();
JSONObject e1 = new JSONObject(); e1.put("val", "hello"); array.add(e1);
JSONObject e2 = new JSONObject(); e2.put("val", "goodbye"); array.add(e2);
JSONObject e3 = new JSONObject(); e3.put("val", "farewell"); array.add(e3);
value.put("greetings", array);
List<JSONObject> inputs = new ArrayList<>();
inputs.add(value);
inputs.add(new JSONObject()); // no list present
JSONObject emptyList = new JSONObject();
emptyList.put("greetings", new JSONArray());
inputs.add(emptyList);
TStream<JSONObject> s = t.constants(inputs);
TStream<JSONObject> jsonm = JSONStreams.flattenArray(s, "greetings");
TStream<String> output = JSONStreams.serialize(jsonm);
completeAndValidate(output, 10, e1.toString(), e2.toString(), e3.toString());
}
示例14: getAuthTokenJSOM
import com.ibm.json.java.JSONObject; //导入方法依赖的package包/类
/**
* {"token_type":"Bearer","expires_in":7200,"access_token":"Qdl8N6KPoyfAX3Lumtgia6leqacd"}
* @return
*/
public String getAuthTokenJSOM()
{
JSONObject jobj = new JSONObject();
jobj.put( KEY_TOKEN_TYPE, _token_type );
if( isError() )
{
jobj.put( "ErrorMessage", getErrorMessage() );
String errorType = "";
switch( getErrorType() )
{
case NONE:
errorType = "None";
break;
case HTTP:
errorType = "HTTP";
break;
case REST:
errorType = "REST";
break;
case API:
errorType = "API";
break;
case EXCEPTION:
errorType = "EXCEPTION";
break;
}
jobj.put( "ErrorType", errorType );
jobj.put( "ErrorCode", getErrorCode());
}
else
{
jobj.put( KEY_TOKEN_TYPE, _token_type );
jobj.put( KEY_EXPIRES_IN, "" + _expires_in );
jobj.put( KEY_ACCESS_TOKEN, _access_token );
}
return jobj.toString();
}
示例15: Builder
import com.ibm.json.java.JSONObject; //导入方法依赖的package包/类
public Builder(String index, String type, boolean enabled) {
this.index = index;
this.type = type;
JSONObject enabledTrue = new JSONObject();
enabledTrue.put(ENABLED_PROPERTY, enabled);
JSONObject sizeMapping = new JSONObject();
sizeMapping.put(SIZE_ATTR, enabledTrue);
this.source = sizeMapping;
}