java - 为什么 break 不结束 while 循环?

标签 java json

我有一些 json:

{"continue":{"continue":"||","rvcontinue":"20021126020855|1194424"},"query":{"pages":{"468997":{"ns":0,"revisions":[{"revid":445765,"comment":"","user":"Nate Silva","parentid":0,"timestamp":"2002-11-26T02:01:24Z"}],"pageid":468997,"title":"Vodafone"}}}}

我想递归地从 json 中解析时间戳:

private static LocalDate getTimeStamp(JSONObject json) throws IOException {
        Iterator<?> keys = json.keys();
        LocalDate ld = null;

        // iterate recursively over all keys from the JSON until current key is
        // timestamp
        while (keys.hasNext()) {
            String key = (String) keys.next();
            if (key.equals("timestamp")) {
                // timezone does not matter, as we just want to extract the date
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
                        "yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
                ld = LocalDate.parse(json.get(key).toString(), formatter);
                System.out.println(ld);
                break;
            }
            // if the current value is another JSON object, call setTimeStamp
            // with value as param
            else if (json.get(key) instanceof JSONObject) {
                getTimeStamp((JSONObject) json.get(key));
            }
            // handle current value being JSON array
            else if (json.get(key) instanceof JSONArray) {
                JSONArray jarray = (JSONArray) json.get(key);
                for (int i = 0; i < jarray.length(); i++) {
                    if (jarray.get(i) instanceof JSONObject) {
                        getTimeStamp((JSONObject) jarray.get(i));
                    }
                }
            }

        }
        System.out.println(ld);
        return ld;
    }

我尝试调试它,但我不明白为什么在我调用 break 后调用该方法。尽管它到达了代码的那部分,但它返回 null 而不是时间戳。

最佳答案

您的代码返回 null 因为您忽略了递归调用的结果。您应该将它们存储在一个变量中,如果它不是 null 则返回它:

while (keys.hasNext() && ld == null) {
    ...
    else if (json.get(key) instanceof JSONObject) {
        ld = getTimeStamp((JSONObject) json.get(key));
    }
    // handle current value being JSON array
    else if (json.get(key) instanceof JSONArray) {
        JSONArray jarray = (JSONArray) json.get(key);
        for (int i = 0; i < jarray.length(); i++) {
            if (jarray.get(i) instanceof JSONObject) {
                ld = getTimeStamp((JSONObject) jarray.get(i));
                if (ld != null) 
                    break;
            }
        }
    }
}

请注意,由于您正在寻找第一个时间戳,因此 while 循环应该在 ld 变为非 null 时停止。

关于java - 为什么 break 不结束 while 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33707649/

相关文章:

javascript - Firebase 数据库规范化

json - Spring MVC 3 + JSON

java-server websocket 失败 : Error during WebSocket handshake: Unexpected response code: 404

java - 在同一范围内调用时变量会产生多个值java

java - Eclipse Tomcat 服务器未启动

php - Laravel 使用等于运算符查询 MySQL JSON 列

java - Runnable 根本不在 fragment 内运行

java - 确定 MQ 消息队列何时为空(所有消息均已读取)

jQuery:将 JSON 解析为元素数组

json - Swift 从 JSON 请求生成通用函数