简体   繁体   English

JSONObject 中的嵌套数组返回为空(org.json)

[英]Nested array in JSONObject is returning as empty (org.json)

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.我从 JSON 文件中得到一个嵌套的 JSON 数组(“投资”),但是当我尝试使用它时,该数组似乎是空的。

My JSON file:我的 JSON 文件:

    {
"invested": [
    {
        "email" : "test@test.com",
        "password" : "test"
    }
],
"notInvested": [
    {
        "email" : "test@test.com",
        "password" : "test"
    }
]}

Here is how I get the JSONObject from file:这是我从文件中获取 JSONObject 的方法:

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());';当我尝试调用'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:我运行程序时返回的错误说我的索引 0 超出了长度 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?我对 JSON 很陌生,所以这仍然有点令人困惑,关于我做错了什么有什么想法吗? 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.编辑:为澄清起见,我试图从 json 文件中的“投资”数组中获取“电子邮件”和“密码”。

The arrays in your sample file only contain one element each.示例文件中的 arrays 每个仅包含一个元素。

JSONArray investedArray = (JSONArray) validLogins.get("invested");
JSONArray notInvestedArray = (JSONArray) validLogins.get("notInvested");

JSON reminder: JSON 提醒:

object: { key : value, key : value, ... }
array:  [ value, value, ... ]

Your sample file is:您的示例文件是:

{ key : array, key : array }

or或者

{ key : [ object ], key : [ object ] }

You are not pulling the email and password out of the object within the JSON array correctly.您没有正确地将 email 和密码从 JSON 阵列中的 object 中拉出。

loginArray contains a JSON array, with one element in it, a JSON object. loginArray包含一个 JSON 数组,其中有一个元素,一个 JSON object。 loginArray.get(0) has the following value: loginArray.get(0)具有以下值:

{
    "email" : "test@test.com",
    "password" : "test"
}

You are attempting to convert this entire object to a string, when you want to be pulling out the values of the individual properties within this object.当您想要提取此 object 中各个属性的值时,您正试图将整个 object 转换为字符串。

To obtain the email address, use loginArray.getJSONObject(0).getString("email") .要获取 email 地址,请使用loginArray.getJSONObject(0).getString("email") This gets the first element out of loginArray as a JSONObject , and then reads the value of the email property of this JSONObject as a string.loginArray中的第一个元素作为JSONObject获取,然后将该JSONObjectemail属性的值作为字符串读取。

To obtain the password, use loginArray.getJSONObject(0).getString("password") .要获取密码,请使用loginArray.getJSONObject(0).getString("password")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM