Java Code Examples for org.json.JSONArray#toList()

The following examples show how to use org.json.JSONArray#toList() . 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: StringUtil.java    From qaf with MIT License 5 votes vote down vote up
/**
 * 
 * @param csv
 * @return
 */
private static Object[] getArrayFromCsv(String csv){
	JSONArray obj = CDL.rowToJSONArray(new JSONTokener(csv));

	List<Object> strings = obj.toList();
	Object[] array = new Object[strings.size()];
	for (int i=0; i<strings.size();i++){
		array[i]=toObject((String) strings.get(i));
	}
	return array;
}
 
Example 2
Source File: CapabilityManager.java    From AppiumTestDistribution with GNU General Public License v3.0 5 votes vote down vote up
private <T> T appiumServerProp(String host, String arg) throws Exception {
    JSONArray hostMachineObject = CapabilityManager.getInstance().getHostMachineObject();
    List<Object> hostIP = hostMachineObject.toList();
    Object machineIP = hostIP.stream().filter(object -> ((Map) object).get("machineIP")
        .toString().equalsIgnoreCase(host)
        && ((Map) object).get(arg) != null)
        .findFirst().orElse(null);
    return (T) ((Map) machineIP).get(arg);
}
 
Example 3
Source File: JsonUtils.java    From codenjoy with GNU General Public License v3.0 5 votes vote down vote up
public static List<String> getStrings(JSONArray array) {
    List<String> result = new LinkedList<>();
    for (Object object : array.toList()) {
        result.add((String)object);
    }
    return result;
}
 
Example 4
Source File: JsonApplier.java    From oxAuth with MIT License 5 votes vote down vote up
public void apply(JSONObject source, Object target, PropertyDefinition property) {
    try {
        if (!source.has(property.getJsonName())) {
            return;
        }
        if (!isAllowed(source, target, property, target.getClass())) {
            return;
        }

        Field field = target.getClass().getDeclaredField(property.getJavaTargetPropertyName());
        field.setAccessible(true);


        Object valueToSet = null;

        if (String.class.isAssignableFrom(property.getJavaType())) {
            valueToSet = source.optString(property.getJsonName());
        }
        if (Collection.class.isAssignableFrom(property.getJavaType())) {
            final JSONArray jsonArray = source.getJSONArray(property.getJsonName());
            valueToSet = jsonArray.toList();
        }

        field.set(target, valueToSet);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}
 
Example 5
Source File: Utils.java    From blobsaver with GNU General Public License v3.0 4 votes vote down vote up
static List<Map<String, Object>> getFirmwareList(String deviceIdentifier) throws IOException {
    String response = makeRequest(new URL("https://api.ipsw.me/v4/device/" + deviceIdentifier));
    JSONArray firmwareListJson = new JSONObject(response).getJSONArray("firmwares");
    @SuppressWarnings("unchecked") List<Map<String, Object>> firmwareList = (List) firmwareListJson.toList();
    return firmwareList;
}
 
Example 6
Source File: JSONArrayTest.java    From JSON-Java-unit-test with Apache License 2.0 4 votes vote down vote up
/**
 * Exercise JSONArray toString() method with various indent levels.
 */
@Test
public void toList() {
    String jsonArrayStr =
            "[" +
                "[1,2," +
                    "{\"key3\":true}" +
                "]," +
                "{\"key1\":\"val1\",\"key2\":" +
                    "{\"key2\":null}," +
                "\"key3\":42,\"key4\":[]" +
                "}," +
                "[" +
                    "[\"value1\",2.1]" +
                "," +
                    "[null]" +
                "]" +
            "]";

    JSONArray jsonArray = new JSONArray(jsonArrayStr);
    List<?> list = jsonArray.toList();

    assertTrue("List should not be null", list != null);
    assertTrue("List should have 3 elements", list.size() == 3);

    List<?> val1List = (List<?>) list.get(0);
    assertTrue("val1 should not be null", val1List != null);
    assertTrue("val1 should have 3 elements", val1List.size() == 3);

    assertTrue("val1 value 1 should be 1", val1List.get(0).equals(Integer.valueOf(1)));
    assertTrue("val1 value 2 should be 2", val1List.get(1).equals(Integer.valueOf(2)));

    Map<?,?> key1Value3Map = (Map<?,?>)val1List.get(2);
    assertTrue("Map should not be null", key1Value3Map != null);
    assertTrue("Map should have 1 element", key1Value3Map.size() == 1);
    assertTrue("Map key3 should be true", key1Value3Map.get("key3").equals(Boolean.TRUE));

    Map<?,?> val2Map = (Map<?,?>) list.get(1);
    assertTrue("val2 should not be null", val2Map != null);
    assertTrue("val2 should have 4 elements", val2Map.size() == 4);
    assertTrue("val2 map key 1 should be val1", val2Map.get("key1").equals("val1"));
    assertTrue("val2 map key 3 should be 42", val2Map.get("key3").equals(Integer.valueOf(42)));

    Map<?,?> val2Key2Map = (Map<?,?>)val2Map.get("key2");
    assertTrue("val2 map key 2 should not be null", val2Key2Map != null);
    assertTrue("val2 map key 2 should have an entry", val2Key2Map.containsKey("key2"));
    assertTrue("val2 map key 2 value should be null", val2Key2Map.get("key2") == null);

    List<?> val2Key4List = (List<?>)val2Map.get("key4");
    assertTrue("val2 map key 4 should not be null", val2Key4List != null);
    assertTrue("val2 map key 4 should be empty", val2Key4List.isEmpty());

    List<?> val3List = (List<?>) list.get(2);
    assertTrue("val3 should not be null", val3List != null);
    assertTrue("val3 should have 2 elements", val3List.size() == 2);

    List<?> val3Val1List = (List<?>)val3List.get(0);
    assertTrue("val3 list val 1 should not be null", val3Val1List != null);
    assertTrue("val3 list val 1 should have 2 elements", val3Val1List.size() == 2);
    assertTrue("val3 list val 1 list element 1 should be value1", val3Val1List.get(0).equals("value1"));
    assertTrue("val3 list val 1 list element 2 should be 2.1", val3Val1List.get(1).equals(Double.valueOf("2.1")));

    List<?> val3Val2List = (List<?>)val3List.get(1);
    assertTrue("val3 list val 2 should not be null", val3Val2List != null);
    assertTrue("val3 list val 2 should have 1 element", val3Val2List.size() == 1);
    assertTrue("val3 list val 2 list element 1 should be null", val3Val2List.get(0) == null);

    // assert that toList() is a deep copy
    jsonArray.getJSONObject(1).put("key1", "still val1");
    assertTrue("val2 map key 1 should be val1", val2Map.get("key1").equals("val1"));

    // assert that the new list is mutable
    assertTrue("Removing an entry should succeed", list.remove(2) != null);
    assertTrue("List should have 2 elements", list.size() == 2);
}