I am getting a nested JSON array ("invested") from within a JSON file, but the array appears to be empty when I try to use it.
My JSON file:
{
"invested": [
{
"email" : "[email protected]",
"password" : "test"
}
],
"notInvested": [
{
"email" : "[email protected]",
"password" : "test"
}
]}
Here is how I get the JSONObject from file:
public JSONObject returnJSONObject(String path) throws JSONException, IOException
{
path = System.getProperty("user.dir") + path;
JSONObject obj = parseJSONFile(path);
return obj;
}
public static JSONObject parseJSONFile(String filename) throws JSONException, IOException
{
String content = new String(Files.readAllBytes(Paths.get(filename)));
Reporter.log(content);
return new JSONObject(content);
}
And here is where it fails. When I try to call 'loginPage.login(loginArray.get(0).toString(), loginArray.get(1).toString());';
JSONObject validLogins = returnJSONObject("valid-user-logins.json");
JSONArray loginArray = (JSONArray) validLogins.get("invested");
// submit valid credentials
loginPage.login(loginArray.get(0).toString(), loginArray.get(1).toString());
The error i get back when I run the program says that my index 0 is out of bounds for length 0:
org.json.JSONException: JSONArray initial value should be a string or collection or array.
I am quite new to JSON so this is all still a bit confusing, Any ideas on what I am doing wrong? Any help would be greatly appreciated!
EDIT:
For clarification, I am trying to grab the "email" & "password" from the "invested" array in the json file.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…