Search in sources :

Example 61 with JsonArray

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());
    }
}
Also used : JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject)

Example 62 with JsonArray

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;
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray)

Example 63 with JsonArray

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);
        }
    }
}
Also used : JSONArray(org.json.simple.JSONArray) JSONObject(org.json.simple.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap)

Example 64 with JsonArray

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);
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) Test(org.junit.Test)

Example 65 with JsonArray

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;
}
Also used : JSONObject(org.json.simple.JSONObject) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) File(java.io.File)

Aggregations

JSONArray (org.json.simple.JSONArray)267 JSONObject (org.json.simple.JSONObject)238 JSONParser (org.json.simple.parser.JSONParser)73 Test (org.junit.Test)40 ParseException (org.json.simple.parser.ParseException)23 ArrayList (java.util.ArrayList)21 HttpClient (org.apache.commons.httpclient.HttpClient)21 IOException (java.io.IOException)17 HashMap (java.util.HashMap)17 GetMethod (org.apache.commons.httpclient.methods.GetMethod)17 Transaction (org.xel.Transaction)12 File (java.io.File)11 List (java.util.List)10 HttpResponse (org.apache.http.HttpResponse)10 BlockchainTest (org.xel.BlockchainTest)10 APICall (org.xel.http.APICall)10 Block (org.xel.Block)9 MapLayer (au.org.emii.portal.menu.MapLayer)8 Map (java.util.Map)8 FileReader (java.io.FileReader)6