use of net.minidev.json.JSONArray in project json-android-compare by martinadamek.
the class SmartJson method parsePublicTimeline.
public List<Map> parsePublicTimeline(InputStream inputStream) {
List<Map> result = new ArrayList<Map>();
JSONParser p = new JSONParser(JSONParser.MODE_JSON_SIMPLE);
try {
Map map;
Set keys;
Set keys2;
JSONObject user;
JSONObject jsonObject;
JSONArray jsonArray = (JSONArray) p.parse(new InputStreamReader(inputStream));
int size = jsonArray.size();
for (int i = 0; i < size; i++) {
map = new HashMap();
jsonObject = (JSONObject) jsonArray.get(i);
keys = jsonObject.keySet();
for (Object key : keys) {
if ("user".equals(key)) {
user = (JSONObject) jsonObject.get(key);
keys2 = user.keySet();
for (Object key2 : keys2) {
map.put("user." + key2, user.get(key2));
}
} else {
map.put(key, jsonObject.get(key));
}
}
result.add(map);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
use of net.minidev.json.JSONArray in project ddf by codice.
the class GeoJsonQueryResponseTransformer method transform.
@Override
public BinaryContent transform(SourceResponse upstreamResponse, Map<String, Serializable> arguments) throws CatalogTransformerException {
if (upstreamResponse == null) {
throw new CatalogTransformerException("Cannot transform null " + SourceResponse.class.getName());
}
JSONObject rootObject = new JSONObject();
addNonNullObject(rootObject, "hits", upstreamResponse.getHits());
JSONArray resultsList = new JSONArray();
if (upstreamResponse.getResults() != null) {
for (Result result : upstreamResponse.getResults()) {
if (result == null) {
throw new CatalogTransformerException("Cannot transform null " + Result.class.getName());
}
resultsList.add(convertToJSON(result));
}
}
addNonNullObject(rootObject, "results", resultsList);
String jsonText = JSONValue.toJSONString(rootObject);
return new BinaryContentImpl(new ByteArrayInputStream(jsonText.getBytes(StandardCharsets.UTF_8)), DEFAULT_MIME_TYPE);
}
use of net.minidev.json.JSONArray in project pentaho-kettle by pentaho.
the class JsonInputTest method testJsonInputPathResolution.
@Test
public void testJsonInputPathResolution() throws KettleException {
JsonInputField inputField = new JsonInputField("value");
final String PATH = "$[*].name";
inputField.setPath(PATH);
inputField.setType(ValueMetaInterface.TYPE_STRING);
JsonInputMeta inputMeta = createSimpleMeta("json", inputField);
VariableSpace variables = new Variables();
JsonInput jsonInput = null;
try {
jsonInput = createJsonInput("json", inputMeta, variables, new Object[] { getSampleJson() });
JsonInputData data = (JsonInputData) Whitebox.getInternalState(jsonInput, "data");
FastJsonReader reader = (FastJsonReader) Whitebox.getInternalState(data, "reader");
RowSet rowset = reader.parse(new ByteArrayInputStream(getSampleJson().getBytes()));
List results = (List) Whitebox.getInternalState(rowset, "results");
JSONArray jsonResult = (JSONArray) results.get(0);
assertEquals(1, jsonResult.size());
assertEquals("United States of America", jsonResult.get(0));
} catch (InvalidPathException pathException) {
assertNull(jsonInput);
}
}
use of net.minidev.json.JSONArray in project mica2 by obiba.
the class DocumentDifferenceService method flatten.
public static Map<String, Object> flatten(Object object) throws JsonProcessingException {
Map<String, Object> map = new RegexHashMap();
if (object == null)
return map;
final String string = mapper.writeValueAsString(object);
JSONArray array = JsonPath.using(Configuration.builder().options(Option.AS_PATH_LIST).build()).parse(string).read("$..*");
array.stream().map(Object::toString).filter(key -> !key.startsWith("$['logo']") && !key.startsWith("$['createdDate']") && !key.startsWith("$['lastModifiedDate']") && !key.startsWith("$['membershipSortOrder']")).forEach(key -> {
Object read = JsonPath.parse(string).read(key);
if (read != null && !(read instanceof Map) && !(read instanceof List)) {
String processedKey = key.replaceAll("(\\$\\[')|('\\])", "").replaceAll("(\\[')", ".");
map.put(processedKey, read);
}
});
return map;
}
use of net.minidev.json.JSONArray in project mica2 by obiba.
the class EntityConfigKeyTranslationService method getTranslationMap.
private RegexHashMap getTranslationMap(EntityConfig config, String prefix) {
String string = config.getSchema();
JSONArray normalArray = JsonPath.using(Configuration.builder().options(Option.AS_PATH_LIST, Option.ALWAYS_RETURN_LIST, Option.SUPPRESS_EXCEPTIONS).build()).parse(string).read("$..*");
JSONArray itemsArray = JsonPath.using(Configuration.builder().options(Option.AS_PATH_LIST, Option.ALWAYS_RETURN_LIST, Option.SUPPRESS_EXCEPTIONS).build()).parse(string).read("$..items");
RegexHashMap map = new RegexHashMap();
String joinedLocales = getJoinedLocales();
normalArray.stream().map(Object::toString).filter(key -> key.endsWith("['title']")).forEach(key -> {
Object read = JsonPath.parse(string).read(key);
if (read != null) {
String cleanKey = key.replaceAll("(\\$\\[')|('\\])", "").replaceAll("(\\[')", ".").replaceAll("\\.title$", "").replaceAll("^properties\\.", "").replaceAll("\\.properties", "");
String processedKey = (cleanKey.startsWith("_") ? prefix : prefix + "model\\.") + Pattern.quote((cleanKey.startsWith("_") ? cleanKey.substring(1) : cleanKey)) + "(\\[\\d+\\])?" + "(" + joinedLocales + ")?";
map.put(processedKey, read.toString());
}
});
itemsArray.stream().map(Object::toString).forEach(itemkey -> {
Object read = JsonPath.parse(string).read(itemkey);
if (read != null && read instanceof List) {
JSONArray array = (JSONArray) read;
array.forEach(arrayItem -> {
if (arrayItem != null && arrayItem instanceof Map) {
Map itemMap = (Map) arrayItem;
Object key = itemMap.get("key");
Object name = itemMap.get("name");
String cleanKey = itemkey.replaceAll("(\\$\\[')|('\\])", "").replaceAll("(\\[')", ".").replaceAll("\\.items$", "").replaceAll("^properties\\.", "").replaceAll("\\.properties", "") + "." + key.toString();
String processedKey = (cleanKey.startsWith("_") ? prefix : prefix + "model\\.") + Pattern.quote((cleanKey.startsWith("_") ? cleanKey.substring(1) : cleanKey));
map.put(processedKey, name);
}
});
}
});
return map;
}
Aggregations