本文整理汇总了Java中org.apache.sling.commons.json.JSONArray.length方法的典型用法代码示例。如果您正苦于以下问题:Java JSONArray.length方法的具体用法?Java JSONArray.length怎么用?Java JSONArray.length使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.sling.commons.json.JSONArray
的用法示例。
在下文中一共展示了JSONArray.length方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testGetSuccessJSON
import org.apache.sling.commons.json.JSONArray; //导入方法依赖的package包/类
@Test
public void testGetSuccessJSON() throws Exception {
final String actual = packageHelper.getSuccessJSON(packageOne);
final JSONObject json = new JSONObject(actual);
assertEquals("success", json.getString("status"));
assertEquals("/etc/packages/testGroup/testPackageName-1.0.0.zip", json.getString("path"));
final String[] expectedFilterSets = new String[]{
"/a/b/c",
"/d/e/f",
"/g/h/i"
};
JSONArray actualArray = json.getJSONArray("filterSets");
for(int i = 0; i < actualArray.length(); i++) {
JSONObject tmp = actualArray.getJSONObject(i);
assertTrue(ArrayUtils.contains(expectedFilterSets, tmp.get("rootPath")));
}
assertEquals(expectedFilterSets.length, actualArray.length());
}
示例2: deserialize
import org.apache.sling.commons.json.JSONArray; //导入方法依赖的package包/类
@Override
public Optional<List<Session>> deserialize(String data) {
final ArrayList<Session> sessions = new ArrayList<>();
try {
final JSONArray array = new JSONArray(data);
for (int i = 0; i < array.length(); i++) {
final JSONObject jsonObject = array.getJSONObject(i);
String name = jsonObject.getString("name");
final ArrayList<FileToOpen> fileToOpens = new ArrayList<>();
final JSONArray filesArray = jsonObject.optJSONArray("filesToOpen");
if (filesArray != null){
for (int j = 0; j < filesArray.length(); j++) {
final JSONObject filesToOpen = filesArray.getJSONObject(j);
String uri = filesToOpen.getString("uri");
Level level = Level.parse(filesToOpen.getString("level"));
OpenMode openMode = OpenMode.valueOf(filesToOpen.getString("openMode"));
String logImporter = filesToOpen.optString("logImporter", null);
fileToOpens.add(new FileToOpen(uri, openMode, level, Optional.ofNullable(logImporter)));
}
}
sessions.add(new Session(name, fileToOpens));
}
} catch (JSONException e) {
LOGGER.error("Can't deserialize sessions: ", e);
Optional.empty();
}
LOGGER.info("Returning deserialized sessions: " + sessions.size());
return Optional.of(sessions);
}
示例3: traverseJSONArray
import org.apache.sling.commons.json.JSONArray; //导入方法依赖的package包/类
/**
* Visit each JSON Object in the JSON Array.
*
* @param jsonArray The JSON Array
*/
protected final void traverseJSONArray(final JSONArray jsonArray) {
if (jsonArray == null) {
return;
}
for (int i = 0; i < jsonArray.length(); i++) {
if (jsonArray.optJSONObject(i) != null) {
this.accept(jsonArray.optJSONObject(i));
} else if (jsonArray.optJSONArray(i) != null) {
this.accept(jsonArray.optJSONArray(i));
}
}
}
示例4: filter
import org.apache.sling.commons.json.JSONArray; //导入方法依赖的package包/类
@SuppressWarnings("squid:S3776")
private void filter(JSONObject typeObject, String resourcePath, ResourceResolver resourceResolver) throws JSONException {
final JSONArray models = typeObject.getJSONArray(KEY_MODELS);
final JSONArray newModels = new JSONArray();
for (int i = 0; i < models.length(); i++) {
final JSONObject modelObject = models.getJSONObject(i);
final String path = modelObject.getString(KEY_MODEL_PATH);
final Resource modelResource = resourceResolver.getResource(path);
if (modelResource != null) {
// we're looking for the appliesTo property on the jcr:content node, the wid value
// is the path to the jcr:content/model node.
final ValueMap properties = modelResource.getParent().getValueMap();
final String[] allowedPaths = properties.get(PN_ALLOWED_PATHS, String[].class);
if (allowedPaths == null) {
newModels.put(modelObject);
} else {
for (final String allowedPath : allowedPaths) {
if (resourcePath.matches(allowedPath)) {
newModels.put(modelObject);
break;
}
}
}
}
}
typeObject.put(KEY_MODELS, newModels);
}
开发者ID:Adobe-Consulting-Services,项目名称:acs-aem-commons,代码行数:29,代码来源:WorkflowModelFilterPageInfoProvider.java
示例5: getResult
import org.apache.sling.commons.json.JSONArray; //导入方法依赖的package包/类
@Override
public Result getResult(String jobId) {
log.debug("getting result for {}", jobId);
Request request = httpClientFactory.get("/speech-to-text/api/v1/recognitions/" + jobId);
try {
JSONObject json = httpClientFactory.getExecutor().execute(request).handleResponse(HANDLER);
log.trace("content: {}", json.toString(2));
if (json.getString("status").equals("completed")) {
JSONArray results = json.getJSONArray("results").getJSONObject(0).getJSONArray("results");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < results.length(); i++) {
JSONObject result = results.getJSONObject(i);
if (result.getBoolean("final")) {
JSONObject firstAlternative = result.getJSONArray("alternatives").getJSONObject(0);
String line = firstAlternative.getString("transcript");
if (StringUtils.isNotBlank(line)) {
double firstTimestamp = firstAlternative.getJSONArray("timestamps").getJSONArray(0).getDouble(1);
builder.append("[").append(firstTimestamp).append("s]: ").append(line).append("\n");
}
}
}
String concatenated = builder.toString();
concatenated = concatenated.replace("%HESITATION ", "");
return new ResultImpl(true, concatenated);
} else {
return new ResultImpl(false, null);
}
} catch (Exception e) {
log.error("Unable to get result. assuming failure.", e);
return new ResultImpl(true, "error");
}
}
示例6: testGetPreviewJSON
import org.apache.sling.commons.json.JSONArray; //导入方法依赖的package包/类
@Test
public void testGetPreviewJSON() throws Exception {
final Set<Resource> resources = new HashSet<Resource>();
resources.add(slingContext.create().resource("/a/b/c"));
resources.add(slingContext.create().resource("/d/e/f"));
resources.add(slingContext.create().resource("/g/h/i"));
final String actual = packageHelper.getPreviewJSON(resources);
final JSONObject json = new JSONObject(actual);
assertEquals("preview", json.getString("status"));
assertEquals("Not applicable (Preview)", json.getString("path"));
final String[] expectedFilterSets = new String[]{
"/a/b/c",
"/d/e/f",
"/g/h/i"
};
JSONArray actualArray = json.getJSONArray("filterSets");
for(int i = 0; i < actualArray.length(); i++) {
JSONObject tmp = actualArray.getJSONObject(i);
assertTrue(ArrayUtils.contains(expectedFilterSets, tmp.get("rootPath")));
}
assertEquals(expectedFilterSets.length, actualArray.length());
}
示例7: testGetPathFilterSetPreviewJSON
import org.apache.sling.commons.json.JSONArray; //导入方法依赖的package包/类
@Test
public void testGetPathFilterSetPreviewJSON() throws Exception {
final List<PathFilterSet> pathFilterSets = new ArrayList<PathFilterSet>();
pathFilterSets.add(new PathFilterSet("/a/b/c"));
pathFilterSets.add(new PathFilterSet("/d/e/f"));
pathFilterSets.add(new PathFilterSet("/g/h/i"));
final String actual = packageHelper.getPathFilterSetPreviewJSON(pathFilterSets);
final JSONObject json = new JSONObject(actual);
assertEquals("preview", json.getString("status"));
assertEquals("Not applicable (Preview)", json.getString("path"));
final String[] expectedFilterSets = new String[]{
"/a/b/c",
"/d/e/f",
"/g/h/i"
};
JSONArray actualArray = json.getJSONArray("filterSets");
for(int i = 0; i < actualArray.length(); i++) {
JSONObject tmp = actualArray.getJSONObject(i);
assertTrue(ArrayUtils.contains(expectedFilterSets, tmp.get("rootPath")));
}
assertEquals(expectedFilterSets.length, actualArray.length());
}
示例8: testParsingOfRealHistoryFile
import org.apache.sling.commons.json.JSONArray; //导入方法依赖的package包/类
@Test
public void testParsingOfRealHistoryFile () throws IOException, JSONException,URISyntaxException {
File jsonFile = new File(TestJsonParser.class.getClassLoader().getSystemResource("TestJsonParser/history.real.json").toURI());
Path jsonPath = jsonFile.toPath();
List<String> jsonLines = Files.readAllLines(jsonPath, Charset.forName("UTF-8")) ;
int size = jsonLines.size() ;
Assert.assertEquals("The size of the file read is not the good one", size, 193398);
StringBuffer fullText = new StringBuffer();
int current = 0 ;
for (String jsonLine : jsonLines ) {
fullText.append(jsonLine.trim());
}
//System.out.println("The big object is created") ;
JSONObject obj = new JSONObject(fullText.toString());
JSONArray arr = obj.getJSONArray("statuses");
boolean foundOneAlfred = false ;
boolean foundOneLaurent = false ;
boolean foundOneAlexandre = false ;
for (int i = 0; i < arr.length(); i++)
{
//System.out.println("i=" + i);
String post = arr.getJSONObject(i).toString();
Assert.assertNotNull("One of the post read is null, json parsing must have an issue", post);
JSONParser parser = new JSONParser(post);
String author = parser.parse("user.name").getString("retValue");
String text = parser.parse("text").getString("retValue");
String geo = parser.parse("geo").getString("retValue");
Assert.assertNotNull(author);
Assert.assertNotNull(text);
Assert.assertNotNull(geo);
//System.out.println("author [" + i + "] =>" + author);
if (author.toLowerCase().contains("laurent")) { foundOneLaurent=true ;}
if (author.toLowerCase().contains("alfred")) { foundOneAlfred= true; }
if (author.toLowerCase().contains("alexandre")) {foundOneAlexandre= true; }
}
Assert.assertTrue("Crazy tweeters are not found" , foundOneLaurent && foundOneAlfred && foundOneAlexandre) ;
}
示例9: getArrayElement
import org.apache.sling.commons.json.JSONArray; //导入方法依赖的package包/类
/**
* Returns the value from the provided array referenced by the given position
* @param array
* The {@link JSONArray} to read the value from
* @param position
* The position to read the value from. Starts with first position at zero
* @return
* The value located at the given position inside the {@link JSONArray}
* @throws JSONException
* Thrown in case accessing the {@link JSONArray} fails for any reason
* @throws NoSuchElementException
* Thrown in case the value of the position parameter points to a non-existing element
* @throws IllegalArgumentException
* Thrown in case the {@link JSONArray} is not a valid instance
*/
protected Object getArrayElement(final JSONArray array, final int position) throws JSONException, NoSuchElementException, IllegalArgumentException {
if(array == null)
throw new IllegalArgumentException("Null is not permitted as input to array parameter");
if(position < 0 || position > array.length()-1)
throw new NoSuchElementException("Referenced position '"+position+"' does not exist in provided array");
return array.get(position);
}
示例10: updateArrayElement
import org.apache.sling.commons.json.JSONArray; //导入方法依赖的package包/类
/**
* Updates the element at the referenced position inside the array using the given value
* @param array
* The {@link JSONArray} to update
* @param position
* The position to update. Starts with first position at zero
* @param value
* The value to insert at the given location
* @throws JSONException
* Thrown in case accessing the {@link JSONArray} fails for any reason
* @throws NoSuchElementException
* Thrown in case the value of the position parameter points to a non-existing element
* @throws IllegalArgumentException
* Thrown in case the {@link JSONArray} is not a valid instance
*/
protected void updateArrayElement(final JSONArray array, final int position, final Serializable value) throws JSONException, NoSuchElementException, IllegalArgumentException {
if(array == null)
throw new IllegalArgumentException("Null is not permitted as input to array parameter");
if(position < 0 || position > array.length()-1)
throw new NoSuchElementException("Referenced position '"+position+"' does not exist in provided array");
array.put(position, value);
}