use of org.json.simple.JsonArray in project jackrabbit-oak by apache.
the class DocumentMKTestBase method parseJSONArray.
protected JSONArray parseJSONArray(String json) throws AssertionError {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(json);
assertTrue(obj instanceof JSONArray);
return (JSONArray) obj;
} catch (Exception e) {
throw new AssertionError("not a valid JSON array: " + e.getMessage());
}
}
use of org.json.simple.JsonArray in project cubrid-manager by CUBRID.
the class MessageUtil method parseJsonResponse.
/**
* <p>
* Parse JSON response message into tree structure.<br>
* Assemble it according to the previous format.
* </p>
*
* @param response String The response string
* @return {@link TreeNode}
*/
@SuppressWarnings("unchecked")
public static TreeNode parseJsonResponse(String response) {
JSONObject responseObj = (JSONObject) JSONValue.parse(response);
// TOOLS-4132 CM can't make the broker_log_top result - fixed by cmserver https api bug
if (responseObj.containsKey("result") && responseObj.containsKey("resultlist") && responseObj.get("result") instanceof JSONArray) {
JSONArray resultlist = new JSONArray();
JSONArray results = (JSONArray) responseObj.get("result");
responseObj.remove("result");
responseObj.remove("resultlist");
for (int i = 0; i < results.size(); i++) {
JSONObject result = new JSONObject();
result.put("result", results.get(i));
resultlist.add(result);
}
responseObj.put("resultlist", resultlist);
} else if (responseObj.containsKey("logstring") && responseObj.containsKey("logstringlist")) {
JSONArray resultlist = new JSONArray();
//(JSONArray) responseObj.get("logstring");
JSONArray results = new JSONArray();
results.add(responseObj.get("logstring"));
responseObj.remove("logstring");
responseObj.remove("logstringlist");
for (int i = 0; i < results.size(); i++) {
JSONObject result = new JSONObject();
result.put("logstring", results.get(i));
resultlist.add(result);
}
responseObj.put("logstringlist", resultlist);
}
TreeNode root = new TreeNode();
parseJson(null, responseObj, root);
return root;
}
use of org.json.simple.JsonArray in project cubrid-manager by CUBRID.
the class MessageUtil method parseJson.
/**
* <p>
* Parse the JSON response message.
* </p>
*
* @param key
* @param value
* @param node
*/
private static void parseJson(String key, Object value, TreeNode node) {
if (value == null && key != null) {
// TODO TEMPORARILY did this
// null --> empty array --> as follows
TreeNode newnode = new TreeNode();
node.addChild(newnode);
node = newnode;
addMsgItem(node, "open:" + key, false);
addMsgItem(node, "close:" + key, false);
node = node.getParent();
} else if (String.class.isAssignableFrom(value.getClass()) && key != null) {
if (key.startsWith("@")) {
key = key.replace("@", "");
// new interface didn't encrypt the @data.
// value = CipherUtils.decrypt(value.toString());
}
addMsgItem(node, key + ":" + value, false);
} else if (Long.class.isAssignableFrom(value.getClass()) && key != null) {
if (key.startsWith("@")) {
key = key.replace("@", "");
// new interface didn't encrypt the @data.
// value = CipherUtils.decrypt(value.toString());
}
addMsgItem(node, key + ":" + value, false);
} else if (value != null && JSONObject.class.isAssignableFrom(value.getClass())) {
// resultlist:start ~ result:start ~ result:end ~ resultlist:end
if ("resultlist".equals(key) || "result".equals(key) || "logstringlist".equals(key)) {
if (key != null) {
addMsgItem(node, key + ":start", false);
}
for (Object responseField : ((JSONObject) value).entrySet()) {
Map.Entry<?, ?> responseKV = (Map.Entry<?, ?>) responseField;
Object innerKey = responseKV.getKey();
Object innerValue = responseKV.getValue();
parseJson(innerKey.toString(), innerValue, node);
}
if (key != null) {
addMsgItem(node, key + ":end", false);
}
} else {
// open:xxx ~ close:xxx
if (key != null) {
TreeNode newnode = new TreeNode();
node.addChild(newnode);
node = newnode;
addMsgItem(newnode, "open:" + key, false);
}
for (Object responseField : ((JSONObject) value).entrySet()) {
Map.Entry<?, ?> responseKV = (Map.Entry<?, ?>) responseField;
Object innerKey = responseKV.getKey();
Object innerValue = responseKV.getValue();
parseJson(innerKey.toString(), innerValue, node);
}
if (key != null) {
addMsgItem(node, "close:" + key, false);
node = node.getParent();
}
}
} else if (value != null && JSONArray.class.isAssignableFrom(value.getClass())) {
for (Object obj : (JSONArray) value) {
parseJson(key, obj, node);
}
}
}
use of org.json.simple.JsonArray in project Gargoyle by callakrsos.
the class JsonTest method jsonTest.
@Test
public void jsonTest() throws ParseException {
JSONArray jsonArray = new JSONArray();
{
JSONObject jsonObject = new JSONObject();
jsonObject.put("svn.url", "http://sample.net");
jsonObject.put("svn.userId", "userId");
jsonObject.put("svn.userPass", "userPass");
jsonArray.add(jsonObject);
}
{
JSONObject jsonObject = new JSONObject();
jsonObject.put("svn.url", "http://sample.ssdsds.net");
jsonObject.put("svn.userId", "userId");
jsonObject.put("svn.userPass", "userPass");
jsonArray.add(jsonObject);
}
System.out.println(jsonArray.toJSONString());
// JSONArray jsonArray2 = new JSONArray();
JSONArray parse = (JSONArray) new JSONParser().parse(jsonArray.toJSONString());
parse.forEach(System.out::println);
}
use of org.json.simple.JsonArray in project cogcomp-nlp by CogComp.
the class QueryMQL method lookupTypeFromTitle.
public List<String> lookupTypeFromTitle(MQLQueryWrapper mql) throws Exception {
List<String> ans = new ArrayList<String>();
JSONObject response;
String mqlQuery = mql.MQLquery;
logger.debug("QUERY IS " + mqlQuery);
String title = mql.value;
String checksum = getMD5Checksum(title);
// first check mid in cache
if (IOUtils.exists(typeCacheLocation + "/" + checksum + ".cached")) {
found++;
logger.info("Found! " + found);
JSONParser jsonParser = new JSONParser();
response = (JSONObject) jsonParser.parse(FileUtils.readFileToString(new File(typeCacheLocation + "/" + checksum + ".cached"), "UTF-8"));
} else {
response = getResponse(mqlQuery);
if (response == null)
return ans;
cacheMiss++;
logger.info("Caching " + cacheMiss);
FileUtils.writeStringToFile(new File(typeCacheLocation + "/" + checksum + ".cached"), response.toString(), "UTF-8");
}
JSONObject result = (JSONObject) response.get("result");
if (result != null) {
JSONArray types = (JSONArray) result.get("type");
for (Object value : types) {
ans.add(value.toString());
}
}
return ans;
}
Aggregations