Java Code Examples for net.sf.json.JSONObject#keys()

The following examples show how to use net.sf.json.JSONObject#keys() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JiraConverter.java    From benten with MIT License 8 votes vote down vote up
public static List<JSONObject> getRequiredFields(JSONObject fields){
    List<JSONObject> requiredFields = new ArrayList<>();

    Iterator<?> iterator = fields.keys();
    while (iterator.hasNext()) {
        String key = (String)iterator.next();
        JSONObject jsonChildObject = fields.getJSONObject(key);

        boolean required = jsonChildObject.getBoolean("required");
        if(required) {
            if(!key.startsWith("customfield")) {
                if ("Project".equals(jsonChildObject.getString("name")) || "Issue Type".equals(jsonChildObject.getString("name"))
                        || "Summary".equals(jsonChildObject.getString("name")))
                    continue;
            }
            jsonChildObject.put("key", key);
            requiredFields.add(jsonChildObject);

        }

    }
    return requiredFields;
}
 
Example 2
Source File: StandardStats.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
/**
 * 递归转换JsonObject
 * @param jsonObject
 * @return
 */
private Map<String, Object> transferMapByJson(JSONObject jsonObject) {
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    for (Iterator keys = jsonObject.keys(); keys.hasNext(); ) {
        String key = String.valueOf(keys.next());
        Object value = jsonObject.get(key);
        if(value instanceof JSONObject){
            JSONObject subJsonObject = (JSONObject) value;
            Map<String, Object> subMap = transferMapByJson(subJsonObject);
            map.put(key,subMap);
        }else{
            map.put(key, value);
        }
    }
    return map;
}
 
Example 3
Source File: JSONHelper.java    From jeewx-api with Apache License 2.0 6 votes vote down vote up
/***
 * 将对象转换为List对象
 * 
 * @param object
 * @return
 */
public static List toArrayList(Object object) {
	List arrayList = new ArrayList();

	JSONArray jsonArray = JSONArray.fromObject(object);

	Iterator it = jsonArray.iterator();
	while (it.hasNext()) {
		JSONObject jsonObject = (JSONObject) it.next();

		Iterator keys = jsonObject.keys();
		while (keys.hasNext()) {
			Object key = keys.next();
			Object value = jsonObject.get(key);
			arrayList.add(value);
		}
	}

	return arrayList;
}
 
Example 4
Source File: JSONHelper.java    From jeecg with Apache License 2.0 6 votes vote down vote up
/***
 * 将对象转换为List<Map<String,Object>>
 * 
 * @param object
 * @return
 */
// 返回非实体类型(Map<String,Object>)的List
public static List<Map<String, Object>> toList(Object object) {
	List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	JSONArray jsonArray = JSONArray.fromObject(object);
	for (Object obj : jsonArray) {
		JSONObject jsonObject = (JSONObject) obj;
		Map<String, Object> map = new HashMap<String, Object>();
		Iterator it = jsonObject.keys();
		while (it.hasNext()) {
			String key = (String) it.next();
			Object value = jsonObject.get(key);
			map.put((String) key, value);
		}
		list.add(map);
	}
	return list;
}
 
Example 5
Source File: JSONHelper.java    From jeewx-api with Apache License 2.0 6 votes vote down vote up
/***
 * 将对象转换为List<Map<String,Object>>
 * 
 * @param object
 * @return
 */
// 返回非实体类型(Map<String,Object>)的List
public static List<Map<String, Object>> toList(Object object) {
	List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	JSONArray jsonArray = JSONArray.fromObject(object);
	for (Object obj : jsonArray) {
		JSONObject jsonObject = (JSONObject) obj;
		Map<String, Object> map = new HashMap<String, Object>();
		Iterator it = jsonObject.keys();
		while (it.hasNext()) {
			String key = (String) it.next();
			Object value = jsonObject.get(key);
			map.put((String) key, value);
		}
		list.add(map);
	}
	return list;
}
 
Example 6
Source File: JSONHelper.java    From jeecg with Apache License 2.0 6 votes vote down vote up
/** 
 * 将json格式的字符串解析成Map对象 <li> 
 * json格式:{"name":"admin","retries":"3fff","testname":"ddd","testretries":"fffffffff"} 
 */  
public static Map<String, List<Map<String, Object>>> json2MapList(String jsonStr)  {  
    Map<String, List<Map<String, Object>>> data = new HashMap<String, List<Map<String, Object>>>();  
    // 将json字符串转换成jsonObject  
    JSONObject jsonObject = JSONObject.fromObject(jsonStr);  
    Iterator it = jsonObject.keys();  
    // 遍历jsonObject数据,添加到Map对象  
    while (it.hasNext())  
    {  
        String key = String.valueOf(it.next());  
        Object value = jsonObject.get(key);  
        List<Map<String, Object>> list = toList(value);
        data.put(key, list);  
    }  
    return data;  
}
 
Example 7
Source File: JSONHelper.java    From jeewx with Apache License 2.0 6 votes vote down vote up
/***
 * 将对象转换为List对象
 * 
 * @param object
 * @return
 */
public static List toArrayList(Object object) {
	List arrayList = new ArrayList();

	JSONArray jsonArray = JSONArray.fromObject(object);

	Iterator it = jsonArray.iterator();
	while (it.hasNext()) {
		JSONObject jsonObject = (JSONObject) it.next();

		Iterator keys = jsonObject.keys();
		while (keys.hasNext()) {
			Object key = keys.next();
			Object value = jsonObject.get(key);
			arrayList.add(value);
		}
	}

	return arrayList;
}
 
Example 8
Source File: JSONHelper.java    From jeewx with Apache License 2.0 6 votes vote down vote up
/***
 * 将对象转换为List<Map<String,Object>>
 * 
 * @param object
 * @return
 */
// 返回非实体类型(Map<String,Object>)的List
public static List<Map<String, Object>> toList(Object object) {
	List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	JSONArray jsonArray = JSONArray.fromObject(object);
	for (Object obj : jsonArray) {
		JSONObject jsonObject = (JSONObject) obj;
		Map<String, Object> map = new HashMap<String, Object>();
		Iterator it = jsonObject.keys();
		while (it.hasNext()) {
			String key = (String) it.next();
			Object value = jsonObject.get(key);
			map.put((String) key, value);
		}
		list.add(map);
	}
	return list;
}
 
Example 9
Source File: JSONHelper.java    From jeecg with Apache License 2.0 6 votes vote down vote up
/***
 * 将对象转换为List对象
 * 
 * @param object
 * @return
 */
public static List toArrayList(Object object) {
	List arrayList = new ArrayList();

	JSONArray jsonArray = JSONArray.fromObject(object);

	Iterator it = jsonArray.iterator();
	while (it.hasNext()) {
		JSONObject jsonObject = (JSONObject) it.next();

		Iterator keys = jsonObject.keys();
		while (keys.hasNext()) {
			Object key = keys.next();
			Object value = jsonObject.get(key);
			arrayList.add(value);
		}
	}

	return arrayList;
}
 
Example 10
Source File: ModelConverterUtils.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
private static Map<String, Object> toMap(JSONObject object) throws JSONException {
    Map<String, Object> map = new HashMap<>();

    Iterator<String> keysItr = object.keys();
    while (keysItr.hasNext()) {
        Object key = keysItr.next();
        Object value = object.get(key);
        if (value instanceof JSONArray) {
            value = toList((JSONArray) value);
        } else if (value instanceof JSONObject) {
            value = toMap((JSONObject) value);
        }
        if (key.toString().contains(SEGMENT_CHARACTER)) {
            String[] split = key.toString().split(ESCAPED_SEGMENT_CHARACTER);
            value = toMap(replaceFirstSegmentOfKey(key.toString()), value);
            Map<String, Object> result = new HashMap<>();
            result.put(split[0], value);
            map = deepMerge(map, result);
        } else {
            map.put(key.toString(), value);
        }
    }
    return map;
}
 
Example 11
Source File: JSONHelper.java    From jeecg with Apache License 2.0 5 votes vote down vote up
public static List<Map<String, Object>> toList(JSONArray jsonArray) {
	List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	for (Object obj : jsonArray) {
		JSONObject jsonObject = (JSONObject) obj;
		Map<String, Object> map = new HashMap<String, Object>();
		Iterator it = jsonObject.keys();
		while (it.hasNext()) {
			String key = (String) it.next();
			Object value = jsonObject.get(key);
			map.put((String) key, value);
		}
		list.add(map);
	}
	return list;
}
 
Example 12
Source File: JSONHelper.java    From jeecg with Apache License 2.0 5 votes vote down vote up
/** 
 * 将json格式的字符串解析成Map对象 <li> 
 * json格式:{"name":"admin","retries":"3fff","testname":"ddd","testretries":"fffffffff"} 
 */  
public static Map<String, Object> json2Map(String jsonStr)  {  
    Map<String, Object> data = new HashMap<String, Object>();  
    // 将json字符串转换成jsonObject  
    JSONObject jsonObject = JSONObject.fromObject(jsonStr);  
    Iterator it = jsonObject.keys();  
    // 遍历jsonObject数据,添加到Map对象  
    while (it.hasNext())  
    {  
        String key = String.valueOf(it.next());  
        Object value = jsonObject.get(key);  
        data.put(key, value);  
    }  
    return data;  
}
 
Example 13
Source File: JSONHelper.java    From jeecg with Apache License 2.0 5 votes vote down vote up
/***
 * 将对象转换为HashMap
 * 
 * @param object
 * @return
 */
public static HashMap toHashMap(Object object) {
	HashMap<String, Object> data = new HashMap<String, Object>();
	JSONObject jsonObject = JSONHelper.toJSONObject(object);
	Iterator it = jsonObject.keys();
	while (it.hasNext()) {
		String key = String.valueOf(it.next());
		Object value = jsonObject.get(key);
		data.put(key, value);
	}

	return data;
}
 
Example 14
Source File: SparkJobServerClientImpl.java    From SparkJobServerClient with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public SparkJobConfig getConfig(String jobId) throws SparkJobServerClientException {
	final CloseableHttpClient httpClient = buildClient();
	try {
		if (!isNotEmpty(jobId)) {
			throw new SparkJobServerClientException("The given jobId is null or empty.");
		}
		HttpGet getMethod = new HttpGet(jobServerUrl + "jobs/" + jobId + "/config");
		getMethod.setConfig(getRequestConfig());
           setAuthorization(getMethod);
		HttpResponse response = httpClient.execute(getMethod);
		String resContent = getResponseContent(response.getEntity());
		JSONObject jsonObj = JSONObject.fromObject(resContent);
		SparkJobConfig jobConfg = new SparkJobConfig();
		Iterator<?> keyIter = jsonObj.keys();
		while (keyIter.hasNext()) {
			String key = (String)keyIter.next();
			jobConfg.putConfigItem(key, jsonObj.get(key));
		}
		return jobConfg;
	} catch (Exception e) {
		processException("Error occurs when trying to get information of the target job config:", e);
	} finally {
		close(httpClient);
	}
	return null;
}
 
Example 15
Source File: SparkJobServerClientImpl.java    From SparkJobServerClient with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public List<SparkJobJarInfo> getJars()
       throws SparkJobServerClientException {
	List<SparkJobJarInfo> sparkJobJarInfos = new ArrayList<>();
	final CloseableHttpClient httpClient = buildClient();
	try {
		HttpGet getMethod = new HttpGet(jobServerUrl + "jars");
		getMethod.setConfig(getRequestConfig());
           setAuthorization(getMethod);
		HttpResponse response = httpClient.execute(getMethod);
		int statusCode = response.getStatusLine().getStatusCode();
		String resContent = getResponseContent(response.getEntity());
		if (statusCode == HttpStatus.SC_OK) {
			JSONObject jsonObj = JSONObject.fromObject(resContent);
			Iterator<?> keyIter = jsonObj.keys();
			while (keyIter.hasNext()) {
				String jarName = (String)keyIter.next();
				String uploadedTime = (String)jsonObj.get(jarName);
				SparkJobJarInfo sparkJobJarInfo = new SparkJobJarInfo();
				sparkJobJarInfo.setJarName(jarName);
				sparkJobJarInfo.setUploadedTime(uploadedTime);
				sparkJobJarInfos.add(sparkJobJarInfo);
			}
		} else {
			logError(statusCode, resContent, true);
		}
	} catch (Exception e) {
		processException("Error occurs when trying to get information of jars:", e);
	} finally {
		close(httpClient);
	}
	return sparkJobJarInfos;
}
 
Example 16
Source File: JSONHelper.java    From jeewx with Apache License 2.0 5 votes vote down vote up
/***
 * 将对象转换为HashMap
 * 
 * @param object
 * @return
 */
public static HashMap toHashMap(Object object) {
	HashMap<String, Object> data = new HashMap<String, Object>();
	JSONObject jsonObject = JSONHelper.toJSONObject(object);
	Iterator it = jsonObject.keys();
	while (it.hasNext()) {
		String key = String.valueOf(it.next());
		Object value = jsonObject.get(key);
		data.put(key, value);
	}

	return data;
}
 
Example 17
Source File: DataGridTag.java    From jeewx with Apache License 2.0 5 votes vote down vote up
/**
 * 生成扩展属性
 * @param field
 * @return
 */
private String extendAttribute(String field) {
	if(StringUtil.isEmpty(field)){
		return "";
	}
	field = dealSyscode(field,1);
	StringBuilder re = new StringBuilder();
	try{
		JSONObject obj = JSONObject.fromObject(field);
		Iterator it = obj.keys();
		while(it.hasNext()){
			String key = String.valueOf(it.next());
			JSONObject nextObj =((JSONObject)obj.get(key));
			Iterator itvalue =nextObj.keys();
			re.append(key+"="+"\"");
			if(nextObj.size()<=1){
				String onlykey = String.valueOf(itvalue.next());
				if("value".equals(onlykey)){
					re.append(nextObj.get(onlykey)+"");
				}else{
					re.append(onlykey+":"+nextObj.get(onlykey)+"");
				}
			}else{
				while(itvalue.hasNext()){
					String multkey = String.valueOf(itvalue.next());
					String multvalue = nextObj.getString(multkey);
					re.append(multkey+":"+multvalue+",");
				}
				re.deleteCharAt(re.length()-1);
			}
			re.append("\" ");
		}
	}catch (Exception e) {
		e.printStackTrace();
		return "";
	}
	return dealSyscode(re.toString(), 2);
}
 
Example 18
Source File: JSONHelper.java    From jeewx-api with Apache License 2.0 5 votes vote down vote up
/***
 * 将对象转换为HashMap
 * 
 * @param object
 * @return
 */
public static HashMap toHashMap(Object object) {
	HashMap<String, Object> data = new HashMap<String, Object>();
	JSONObject jsonObject = JSONHelper.toJSONObject(object);
	Iterator it = jsonObject.keys();
	while (it.hasNext()) {
		String key = String.valueOf(it.next());
		Object value = jsonObject.get(key);
		data.put(key, value);
	}

	return data;
}
 
Example 19
Source File: SparkJobServerClientImpl.java    From SparkJobServerClient with Apache License 2.0 4 votes vote down vote up
/**
 * Generates an instance of <code>SparkJobResult</code> according to the given contents.
 * 
 * @param resContent the content of a http response
 * @return the corresponding <code>SparkJobResult</code> instance
 * @throws Exception error occurs when parsing the http response content
 */
private SparkJobResult parseResult(String resContent) throws Exception {
	JSONObject jsonObj = JSONObject.fromObject(resContent);
	SparkJobResult jobResult = new SparkJobResult(resContent);
	boolean completed = false;
	if(jsonObj.has(SparkJobBaseInfo.INFO_KEY_STATUS)) {
		jobResult.setStatus(jsonObj.getString(SparkJobBaseInfo.INFO_KEY_STATUS));
		if (SparkJobBaseInfo.COMPLETED.contains(jobResult.getStatus())) {
			completed = true;
		}
	} else {
		completed = true;
	}
	if (completed) {
		//Job finished with results
		jobResult.setResult(jsonObj.get(SparkJobBaseInfo.INFO_KEY_RESULT).toString());
	} else if (containsAsynjobStatus(jsonObj)) {
		//asynchronously started job only with status information
		setAsynjobStatus(jobResult, jsonObj);
	} else if (containsErrorInfo(jsonObj)) {
		String errorKey = null;
		if (jsonObj.containsKey(SparkJobBaseInfo.INFO_STATUS_ERROR)) {
			errorKey = SparkJobBaseInfo.INFO_STATUS_ERROR;
		} else if (jsonObj.containsKey(SparkJobBaseInfo.INFO_KEY_RESULT)) {
			errorKey = SparkJobBaseInfo.INFO_KEY_RESULT;
		} 
		//Job failed with error details
		setErrorDetails(errorKey, jsonObj, jobResult);
	} else {
		//Other unknown kind of value needs application to parse itself
		Iterator<?> keyIter = jsonObj.keys();
		while (keyIter.hasNext()) {
			String key = (String)keyIter.next();
			if (SparkJobInfo.INFO_KEY_STATUS.equals(key)) {
				continue;
			}
			jobResult.putExtendAttribute(key, jsonObj.get(key));
		}
	}
	return jobResult;
}
 
Example 20
Source File: RecordPermissionsRest.java    From mobi with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Converts a record policy abridged JSON into a XACML policy object.
 *
 * @param json The record policy JSON
 * @param policyType the PolicyType from the record policy
 * @return The updated XACMLPolicy object
 */
private XACMLPolicy recordJsonToPolicy(String json, PolicyType policyType) {
    String masterBranch = policyType.getRule().get(4).getTarget().getAnyOf().get(0).getAllOf().get(0).getMatch()
            .get(1).getAttributeValue().getContent().get(0).toString();
    policyType.getRule().clear();

    AttributeDesignatorType actionIdAttrDesig = createActionIdAttrDesig();
    AttributeDesignatorType subjectIdAttrDesig = createSubjectIdAttrDesig();
    AttributeDesignatorType groupAttrDesig = createGroupAttrDesig();
    AttributeDesignatorType branchAttrDesig = createBranchAttrDesig();
    AttributeValueType branchAttrVal = createAttributeValue(XSD.STRING, masterBranch);

    JSONObject jsonObject = JSONObject.fromObject(json);
    Iterator<?> keys = jsonObject.keys();

    while (keys.hasNext()) {
        String key = (String)keys.next();
        TargetType target = new TargetType();
        RuleType rule = createRule(EffectType.PERMIT, key, target);
        AttributeValueType ruleTypeAttrVal = new AttributeValueType();
        ruleTypeAttrVal.setDataType(XSD.STRING);

        // Setup Rule Type
        MatchType ruleTypeMatch = createMatch(XACML.STRING_EQUALS, actionIdAttrDesig, ruleTypeAttrVal);
        ruleTypeAttrVal.getContent().clear();
        switch (key) {
            case "urn:read":
                ruleTypeAttrVal.getContent().add(Read.TYPE);
                break;
            case "urn:delete":
                ruleTypeAttrVal.getContent().add(Delete.TYPE);
                break;
            case "urn:update":
                ruleTypeAttrVal.getContent().add(Update.TYPE);
                break;
            case "urn:modify":
                ruleTypeAttrVal.getContent().add("http://mobi.com/ontologies/catalog#Modify");
                break;
            case "urn:modifyMaster":
                ruleTypeAttrVal.getContent().add("http://mobi.com/ontologies/catalog#Modify");
                break;
            default:
                throw new MobiException("Invalid rule key: " + key);
        }

        AnyOfType anyOfType = new AnyOfType();
        AllOfType allOfType = new AllOfType();
        anyOfType.getAllOf().add(allOfType);
        allOfType.getMatch().add(ruleTypeMatch);

        if (key.equalsIgnoreCase("urn:modifyMaster")) {
            MatchType branchMatch = createMatch(XACML.STRING_EQUALS, branchAttrDesig, branchAttrVal);
            allOfType.getMatch().add(branchMatch);
        }
        rule.getTarget().getAnyOf().add(anyOfType);

        // Setup Users
        AnyOfType usersGroups = new AnyOfType();
        JSONObject jsonRule = jsonObject.optJSONObject(key);
        if (jsonRule == null) {
            throw new IllegalArgumentException("Invalid JSON representation of a Policy. Missing rule " + key);
        }
        if (jsonRule.opt("everyone") == null) {
            throw new IllegalArgumentException("Invalid JSON representation of a Policy. Missing everyone field"
                    + "for " + key);
        }
        if (jsonRule.getBoolean("everyone")) {
            MatchType userRoleMatch = createUserRoleMatch();
            AllOfType everyoneAllOf = new AllOfType();
            everyoneAllOf.getMatch().add(userRoleMatch);
            usersGroups.getAllOf().add(everyoneAllOf);
        } else {
            JSONArray usersArray = jsonRule.optJSONArray("users");
            JSONArray groupsArray = jsonRule.optJSONArray("groups");
            if (usersArray == null || groupsArray == null) {
                throw new IllegalArgumentException("Invalid JSON representation of a Policy."
                        + " Users or groups not set properly for " + key);
            }
            addUsersOrGroupsToAnyOf(usersArray, subjectIdAttrDesig, usersGroups);
            addUsersOrGroupsToAnyOf(groupsArray, groupAttrDesig, usersGroups);
        }
        rule.getTarget().getAnyOf().add(usersGroups);

        if (key.equalsIgnoreCase("urn:modify")) {
            ConditionType condition = new ConditionType();
            condition.setExpression(createMasterBranchExpression(masterBranch));
            rule.setCondition(condition);
        }
        policyType.getRule().add(rule);
    }

    return policyManager.createPolicy(policyType);
}