Java Code Examples for net.sf.json.JSONArray#iterator()

The following examples show how to use net.sf.json.JSONArray#iterator() . 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: 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 2
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 3
Source File: Json.java    From xunxian with Apache License 2.0 6 votes vote down vote up
/**
 * StringToMap这两个,都加入两个判断的变量,map.put("xnx3_json_if","1/0");	,1为正常,成功,0为失败,没有正确获取或获取的字符串出错
 * 接受的字符串为: 	[{"a":"aaa","b":"bb"}]
 */

@SuppressWarnings("unchecked")
public Map<String, String> StringToMap(String xnx3_input){
	JSONArray json=JSONArray.fromObject(xnx3_input);
	Map<String, String> map=null;
	Iterator<Map<String, String>> iter=json.iterator();
	if(iter.hasNext()){
		map=iter.next();
		map.put("xnx3_json_if", "1");
	}else{
		map=new HashMap<String, String>();
		map.put("xnx3_json_if", "0");
	}
	json=null;
	return map;
}
 
Example 4
Source File: Json.java    From xunxian with Apache License 2.0 6 votes vote down vote up
/**
 * 将Json格式的String数据转换为List类型
 * @param args:传入纯文本数据,如[{"a":"aaa","b":"bb"}]
 * 
 */
public List<String[]> StringToList(String xnx3_input){
	JSONArray json=JSONArray.fromObject(xnx3_input);
	List<String[]> list=new ArrayList<String[]>();
	Iterator<Map<String, String>> iter=json.iterator();
	if(iter.hasNext()){
		Map<String, String> map=iter.next();
		Iterator i=map.entrySet().iterator();
		
		while(i.hasNext()){//只遍历一次,速度快
			Map.Entry e=(Map.Entry)i.next();
			System.out.println(e.getKey());
			String[] data=new String[2];
			data[0]=e.getKey().toString();
			data[1]=e.getValue().toString();
			list.add(data);
			data=null;
		}
	}
	json=null;
	return list;
	
}
 
Example 5
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 6
Source File: GangliaMetricService.java    From Hue-Ctrip-DI with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private synchronized void initClusterInfo(boolean force) {
	if (clusterHostsMap.isEmpty() || metricMap.isEmpty() || force) {
		String json = UrlUtils.getContent(hostUrl);
		JSONObject jsonObject = JSONObject.fromObject(json);
		JSONObject messagejson = (JSONObject) jsonObject.get("message");
		JSONObject clustersJson = (JSONObject) messagejson.get("clusters");

		for (String cluster : (Set<String>) clustersJson.keySet()) {
			try {

				JSONArray hostArray = (JSONArray) clustersJson.get(cluster);
				List<String> hostList = new ArrayList<String>();
				Iterator<String> iterator = hostArray.iterator();
				while (iterator.hasNext()) {
					String host = iterator.next();
					hostList.add(host);
				}
				if (hostList.size() != 0) {
					clusterHostsMap.put(cluster, hostList);
				}

				List<String> metricList = httpParser
						.getGangliaAttribute(cluster);
				if (metricList != null && metricList.size() != 0) {
					metricMap.put(cluster, metricList);
				}
			} catch (Exception e) {
				logger.error("Ganglia Http Parser Error", e);
				throw new RuntimeException("Ganglia Http Parser Error", e);
			}
		}
	}

}
 
Example 7
Source File: SparkJobServerClientImpl.java    From SparkJobServerClient with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public List<String> getContexts() throws SparkJobServerClientException {
	List<String> contexts = new ArrayList<String>();
	final CloseableHttpClient httpClient = buildClient();
	try {
		HttpGet getMethod = new HttpGet(jobServerUrl + "contexts");
		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) {
			JSONArray jsonArray = JSONArray.fromObject(resContent);
			Iterator<?> iter = jsonArray.iterator();
			while (iter.hasNext()) {
				contexts.add((String)iter.next());
			}
		} else {
			logError(statusCode, resContent, true);
		}
	} catch (Exception e) {
		processException("Error occurs when trying to get information of contexts:", e);
	} finally {
		close(httpClient);
	}
	return contexts;
}
 
Example 8
Source File: SparkJobServerClientImpl.java    From SparkJobServerClient with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public List<SparkJobInfo> getJobs() throws SparkJobServerClientException {
	List<SparkJobInfo> sparkJobInfos = new ArrayList<SparkJobInfo>();
	final CloseableHttpClient httpClient = buildClient();
	try {
		HttpGet getMethod = new HttpGet(jobServerUrl + "jobs");
		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) {
			JSONArray jsonArray = JSONArray.fromObject(resContent);
			Iterator<?> iter = jsonArray.iterator();
			while (iter.hasNext()) {
				JSONObject jsonObj = (JSONObject)iter.next();
				SparkJobInfo jobInfo = createSparkJobInfo(jsonObj);
				sparkJobInfos.add(jobInfo);
			}
		} else {
			logError(statusCode, resContent, true);
		}
	} catch (Exception e) {
		processException("Error occurs when trying to get information of jobs:", e);
	} finally {
		close(httpClient);
	}
	return sparkJobInfos;
}
 
Example 9
Source File: SparkJobServerClientImpl.java    From SparkJobServerClient with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public List<SparkJobInfo> getJobsByStatus(String jobStatus) throws SparkJobServerClientException {
    if (!INFO_JOBS_STATUS.contains(jobStatus.toUpperCase())) {
        throw new SparkJobServerClientException("Invalid Job Status " +
	jobStatus + ". Supported Job Status : " +
	INFO_JOBS_STATUS.toString());
    }
    List<SparkJobInfo> sparkJobInfos = new ArrayList<SparkJobInfo>();
    final CloseableHttpClient httpClient = buildClient();
    try {
        HttpGet getMethod = new HttpGet(jobServerUrl + "jobs?status=" + jobStatus);
        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) {
            JSONArray jsonArray = JSONArray.fromObject(resContent);
            Iterator<?> iter = jsonArray.iterator();
            while (iter.hasNext()) {
                JSONObject jsonObj = (JSONObject)iter.next();
                SparkJobInfo jobInfo = createSparkJobInfo(jsonObj);
                sparkJobInfos.add(jobInfo);
            }
        } else {
            logError(statusCode, resContent, true);
        }
    } catch (Exception e) {
        processException("Error occurs when trying to get information of jobs:", e);
    } finally {
        close(httpClient);
    }
    return sparkJobInfos;
}
 
Example 10
Source File: DirectionsService.java    From jdal with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
private MultiLineString parseResponse(Object response) throws IOException {
	
	List<LineString> lines = new ArrayList<LineString>();
	
	String jsonString = responseToString(response);
	JSONObject  json =  JSONObject.fromObject(jsonString);
	
	if ("OK".equals(json.get("status"))) { 
		JSONArray routes = json.getJSONArray("routes");
		JSONArray legs = routes.getJSONObject(0).getJSONArray("legs");
		JSONArray steps = legs.getJSONObject(0).getJSONArray("steps");
		
		if (log.isDebugEnabled())
			log.debug("Get route with " + steps.size() + " steps");
		
		Iterator iter = steps.iterator();
		
		while (iter.hasNext()) {
			JSONObject step = (JSONObject) iter.next();
			String polyline = step.getJSONObject("polyline").getString("points");
			LineString line = decodePolyline(polyline);
			lines.add(line);
		}
	}
	
	return new MultiLineString(lines.toArray(new LineString[] {}), factory);
}
 
Example 11
Source File: Freebase.java    From BotLibre with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Extract the relevant data from the Freebase property.
 */
@SuppressWarnings("rawtypes")
public List<Object> extractPropertyValues(Object data, List<String> filters, int cascade, Network network, Map<String, Vertex> processed) {
	List<Object> values = new ArrayList<Object>();
	Set<Object> valuesSet = new HashSet<Object>();
	if (data instanceof JSONObject) {
		if (((JSONObject)data).get("values") == null) {
			return values;
		}
		JSONArray array = ((JSONObject)data).getJSONArray("values");
		Object type = ((JSONObject)data).get("valuetype");
		for (Iterator iterator = array.iterator(); iterator.hasNext(); ) {
			Object value = iterator.next();
			if (value instanceof JSONObject) {
				if (type.equals("compound")) {
					value = processCompoundRelationship((JSONObject)value, cascade, network, processed);
					if ((value != null) && !valuesSet.contains(value)) {
						valuesSet.add(value);
						values.add(value);
					}
				} else {
					Object id = ((JSONObject)value).get("id");
					if (id instanceof String) {
						// Filter ids.
						String domain = extractDomain((String)id);
						if (filters.contains(domain)) {
							continue;
						}
						// Freebase seems to be use 'm' to donate topics.
						if ((cascade > 0) && domain.equals("m")) {
							Vertex nested = processId((String)id, cascade - 1, false, "", network, processed);
							if (!valuesSet.contains(nested)) {
								valuesSet.add(nested);
								values.add(nested);
							}
							continue;
						}
					}
					Object text = ((JSONObject)value).get("value");
					if (text == null) {
						text = ((JSONObject)value).get("text");
					}
					if (text != null) {
						if (!valuesSet.contains(text)) {
							valuesSet.add(text);
							values.add(text);
						}
					}
				}
			}
		}
	}
		
	return values;
}
 
Example 12
Source File: JsonTools.java    From sso-oauth2 with Apache License 2.0 3 votes vote down vote up
/**
 * 
* json转换list.
* <br>详细说明
* @param jsonStr json字符串
* @return
* @return List<Map<String,Object>> list
* @throws
* @author slj
* @date 2013年12月24日 下午1:08:03
 */
public static List parseJSON2List(String jsonStr) {
	JSONArray jsonArr = JSONArray.fromObject(jsonStr);
	List list = new ArrayList();
	Iterator<JSONObject> it = jsonArr.iterator();
	while (it.hasNext()) {
		JSONObject json2 = it.next();
		list.add(parseJSON2Map(json2.toString()));
	}
	return list;
}
 
Example 13
Source File: JsonTools.java    From sso-oauth2 with Apache License 2.0 3 votes vote down vote up
/**
 * 
* json转换list.
* <br>详细说明
* @param jsonStr json字符串
* @return
* @return List<Map<String,Object>> list
* @throws
* @author slj
* @date 2013年12月24日 下午1:08:03
 */
public static List parseJSON2List(String jsonStr) {
	JSONArray jsonArr = JSONArray.fromObject(jsonStr);
	List list = new ArrayList();
	Iterator<JSONObject> it = jsonArr.iterator();
	while (it.hasNext()) {
		JSONObject json2 = it.next();
		list.add(parseJSON2Map(json2.toString()));
	}
	return list;
}