本文整理汇总了Java中com.ibm.json.java.JSONArray.add方法的典型用法代码示例。如果您正苦于以下问题:Java JSONArray.add方法的具体用法?Java JSONArray.add怎么用?Java JSONArray.add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.ibm.json.java.JSONArray
的用法示例。
在下文中一共展示了JSONArray.add方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getComments
import com.ibm.json.java.JSONArray; //导入方法依赖的package包/类
private JSONArray getComments(String itemNumber) {
JSONArray returnArray = new JSONArray();
CouchDbConnector dbc = _db.createConnector(dbname, true);
Map<String, String> doc = new HashMap<String, String>();
ViewQuery query = new ViewQuery().allDocs().includeDocs(true);
List<Map> result = dbc.queryView(query, Map.class);
JSONArray jsonresult = new JSONArray();
for (Map element : result) {
JSONObject obj = new JSONObject();
obj.putAll(element);
if(itemNumber==null || obj.get("itemNumber").equals(itemNumber))
jsonresult.add(obj);
}
System.out.println(jsonresult.toString());
return jsonresult;
}
示例2: buildContent
import com.ibm.json.java.JSONArray; //导入方法依赖的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: buildBeaconPayload
import com.ibm.json.java.JSONArray; //导入方法依赖的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;
}
示例4: apply
import com.ibm.json.java.JSONArray; //导入方法依赖的package包/类
@Override
public JSONObject apply(List<String> v) {
JSONObject agg = new JSONObject();
JSONArray items = new JSONArray();
for (String e : v)
items.add(e);
agg.put("items", items);
long ts = System.currentTimeMillis();
agg.put("ts", ts);
if (lastts != 0)
agg.put("delta", ts - lastts);
lastts = ts;
agg.put("count", ++count);
return agg;
}
示例5: testFlatten
import com.ibm.json.java.JSONArray; //导入方法依赖的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());
}
示例6: testFlattenNoObjects
import com.ibm.json.java.JSONArray; //导入方法依赖的package包/类
@Test
public void testFlattenNoObjects() throws Exception {
final Topology t = new Topology();
final JSONObject value = new JSONObject();
final JSONArray array = new JSONArray();
array.add("hello");
value.put("greetings", array);
TStream<JSONObject> s = t.constants(Collections.singletonList(value));
TStream<JSONObject> jsonm = JSONStreams.flattenArray(s, "greetings");
TStream<String> output = JSONStreams.serialize(jsonm);
JSONObject payload = new JSONObject();
payload.put("payload", "hello");
completeAndValidate(output, 10, payload.toString());
}
示例7: parseForRegularExpression
import com.ibm.json.java.JSONArray; //导入方法依赖的package包/类
/**
* This method extracts personal data using regular expressions
*
* @param text Text document from which text matching regular expressions need to be extracted
* @param nluJSON NLUOutput
* @return nluJSON Regular expression parsing results are appended to the input NLU output and returned
*/
public JSONObject parseForRegularExpression(String text, JSONObject nluJSON) {
// get, from config, what entity types to be used for regular expression
String regexEntityTypesConfig = System.getenv("regex_params");
String[] regexEntityTypes = regexEntityTypesConfig.split(",");
// extract string from text for each regex and build JSONObjects for
// them
JSONArray entities = (JSONArray) nluJSON.get("entities");
if (entities == null) {
return new JSONObject();
}
if (regexEntityTypes != null && regexEntityTypes.length > 0) {
for (int i = 0; i < regexEntityTypes.length; i++) {
String regexEntityType = regexEntityTypes[i];
// Get regular expression got this entity type
String regex = System.getenv(regexEntityType + "_regex");
// Now get extracted values from text
// getting is as a list
List<String> matchResultList = getRegexMatchingWords(text, regex);
// Add entries in this regex to entities in nluOutput, for each
// result
// First build a JSONObject
if (matchResultList != null && matchResultList.size() > 0) {
for (int j = 0; j < matchResultList.size(); j++) {
String matchResult = matchResultList.get(j);
JSONObject entityEntry = new JSONObject();
entityEntry.put("type", regexEntityType);
entityEntry.put("text", matchResult);
entities.add(entityEntry);
}
}
}
}
return nluJSON;
}
示例8: testFlattenWithAttributes
import com.ibm.json.java.JSONArray; //导入方法依赖的package包/类
@Test
public void testFlattenWithAttributes() throws Exception {
final Topology t = new Topology();
JSONObject e1 = new JSONObject(); e1.put("val", "hello");
JSONObject e2 = new JSONObject(); e2.put("val", "goodbye"); e2.put("a", "def");
final JSONObject value = new JSONObject();
{
value.put("a", "abc");
final JSONArray array = new JSONArray();
array.add(e1);
array.add(e2);
value.put("greetings", array);
}
List<JSONObject> inputs = new ArrayList<>();
inputs.add(value);
final JSONObject value2 = new JSONObject();
{
final JSONArray array2 = new JSONArray();
array2.add(e1.clone());
array2.add(e2.clone());
value2.put("greetings", array2);
}
inputs.add(value2);
TStream<JSONObject> s = t.constants(inputs);
TStream<JSONObject> jsonm = JSONStreams.flattenArray(s, "greetings", "a");;
TStream<String> output = JSONStreams.serialize(jsonm);
JSONObject e1r = (JSONObject) e1.clone();
e1r.put("a", "abc");
assertFalse(e1.containsKey("a"));
completeAndValidate(output, 10, e1r.toString(), e2.toString(), e1.toString(), e2.toString());
}
示例9: bucketGrantRightsV2
import com.ibm.json.java.JSONArray; //导入方法依赖的package包/类
public Result bucketGrantRightsV2(
String bucketKey,
String serviceId,
String access
)
throws IOException,
URISyntaxException
{
String scope[] = { SCOPE_BUCKET_UPDATE };
ResultAuthentication authResult = authenticate( scope );
if( authResult.isError() )
{
return authResult;
}
/*
{
"allow":[
{"authId":"D6C9x7Bk0vo2HA1qC7l0VHM4MtYqZsN4","access":"full"}
]
}
*/
JSONObject j_rights = new JSONObject();
j_rights.put( "authId", serviceId );
j_rights.put( "access", access );
JSONArray j_serviceList = new JSONArray();
j_serviceList.add( j_rights );
JSONObject j_grantRequest = new JSONObject();
j_grantRequest.put( "allow", j_serviceList );
String jStr = j_grantRequest.toString();
String params[] = { bucketKey.toLowerCase() };
String frag = makeURN( API_OSS, PATT_BUCKET_GRANT2, params );
URI uri = new URI( _protocol, null, lookupHostname(), _port, frag , null, null );
URL url = new URL( uri.toASCIIString() );
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod( "POST" );
authResult.setAuthHeader( connection );
connection.setRequestProperty( "Accept", "Application/json" );
connection.setRequestProperty( "Content-Length", "" + jStr.length() );
connection.setRequestProperty( "Content-Type", "application/json; charset=utf-8" );
connection.setDoOutput( true );
OutputStream os = null;
try
{
os = connection.getOutputStream();
os.write(jStr.getBytes(Charset.forName("UTF-8")));
}
finally
{
if( os != null ) os.close();
}
Result result = new Result( connection );
return result;
}
示例10: bucketRevokeRightsV2
import com.ibm.json.java.JSONArray; //导入方法依赖的package包/类
public Result bucketRevokeRightsV2(
String bucketKey,
String serviceId
)
throws IOException,
URISyntaxException
{
String scope[] = { SCOPE_BUCKET_UPDATE };
ResultAuthentication authResult = authenticate( scope );
if( authResult.isError() )
{
return authResult;
}
/*
{
"revoke":[
{"authId":"2s23v1A2uIHPNJQ2nlJjIxYKqAAOInjY"}
]
}
*/
JSONObject j_rights = new JSONObject();
j_rights.put( "authId", serviceId );
JSONArray j_serviceList = new JSONArray();
j_serviceList.add( j_rights );
JSONObject j_grantRequest = new JSONObject();
j_grantRequest.put( "revoke", j_serviceList );
String jStr = j_grantRequest.toString();
String params[] = { bucketKey.toLowerCase() };
String frag = makeURN( API_OSS, PATT_BUCKET_REVOKE2, params );
URI uri = new URI( _protocol, null, lookupHostname(), _port, frag, null, null );
URL url = new URL( uri.toASCIIString() );
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
authResult.setAuthHeader( connection );
connection.setRequestMethod( "POST" );
connection.setRequestProperty( "Content-Type", "application/json; charset=utf-8" );
connection.setDoOutput( true );
OutputStream os = null;
try
{
os = connection.getOutputStream();
os.write(jStr.getBytes(Charset.forName("UTF-8")));
}
finally
{
if( os != null ) os.close();
}
Result result = new Result( connection );
return result;
}