[Java]:无法解析没有数组/子数组的JSON

时间:2017-01-10 07:04:16

标签: java arrays json

我正在使用json-simple-1.1.jar 并尝试从json中获取多个(至少3个)值,如代码部分所示。

我的代码适用于具有多个数组的JSON,但不适用于简单的json格式。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(getLayoutResource());
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);
    }
}

以下是我尝试使用的java逻辑:

{
"Addresses": {
    "UserName": "Rahul", 
    "Status": "Active", 
    "CreateDate": "2017-01-09T11:39:31.244Z", 
    "SecretAccessKey": "ABCD-EFGH-HIJK", 
    "AccessKeyId": "1234567"
 }
}

我收到错误:" org.json.simple.JSONObject无法转换为org.json.simple.JSONArray"在下面一行:

public static String[] getValuesFromJson(String filename, Object key, int exp_sizeOfArray, String[] exp_keys) throws FileNotFoundException, IOException, ParseException { 

    String valuesFromJson[] = new String[exp_keys.length]; 

    /** Create a JSONParser object*/
    JSONParser parser = new JSONParser();

    /** Read the JSON file using parser*/
    JSONObject jsonObject = (JSONObject) parser.parse(new FileReader(
            filename));

    System.out.println("JsonObject size: "+jsonObject.keySet().size());
    for (Object object : jsonObject.keySet()) {
        System.out.println(jsonObject.get(object.toString()));
    }
    /** Get the values in JSONArray using the key*/
    JSONArray jsonArray = (JSONArray) jsonObject.get(key);
    for (int i = 0; i < jsonArray.size(); i++) {

        /** Add the individual set from JSONArray to a JSONObject */
        JSONObject subJsonObject = (JSONObject) parser.parse(jsonArray.get(i).toString());

        /** Check for expected size of array */
        if(subJsonObject.size() <= exp_sizeOfArray){
            int index=0;
            /** Check for each key value in the sub-JSONObject keySet*/
            for (Object object : subJsonObject.keySet()) {

                /** Iterate until the expected key value matches with the actual value*/
                for (int j = 0; j < exp_keys.length; j++) {

                    /** Check if the expected key matches with any of the key value*/
                    if(object.toString().trim().equals(exp_keys[j].toString().trim())){
                        System.out.println("Key: '".concat(object.toString()).concat("'\tValue: '").concat(subJsonObject.get(object)+"'"));
                        valuesFromJson[index] = subJsonObject.get(exp_keys[j]).toString();
                        index++;
                        break;
                    }
                }
            } 
        }
    }

    /** Return the value of expected key*/
    return valuesFromJson;
}

2 个答案:

答案 0 :(得分:1)

您正在尝试将JSONObject投射到JSONArray,但没有数组。而是获取所有对象键并迭代它。

如果你去json.org(1),你可以在那里找到:

数组是有序的值集合。数组以[(左括号)开头,以]结尾(右括号)。值以(逗号)

分隔

答案 1 :(得分:0)

瞧! 在未将JSONObject转换为JSONArray

的情况下完成此操作
JSONObject jsonObject = (JSONObject) parser.parse(new FileReader(
            filename));
    /** Creating another JSONObject here */
    JSONObject jsonObject2 = (JSONObject) parser.parse(jsonObject.get(key).toString());
    for (Object newKey : jsonObject2.keySet()) {
        System.out.println("Key: '".concat(newKey.toString()).concat("'\tValue: '").concat(jsonObject2.get(newKey)+"'"));
    }

谢谢你们的帮助!