简体   繁体   English

为什么中断不结束while循环?

[英]Why does break not end the while loop?

I have some 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"}}}}

I want to parse the timestamp from the json recursively: 我想从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;
    }

I tried debugging it and I don't get why the method is called after I invoke break . 我尝试调试它,但我不明白为什么在调用break之后调用该方法。 It returns null instead of the timestamp although it reaches that part of the code. 尽管它到达了代码的那部分,但它返回的是null而不是时间戳。

The "timestamp" key is nested inside your JSON, so it takes a few recursive calls of your method to get to it. "timestamp"键嵌套在JSON中,因此需要对方法进行一些递归调用。

When you make a recursive call, you should use the value returned by those calls. 进行递归调用时,应使用这些调用返回的值。

change 更改

getTimeStamp((JSONObject) json.get(key));

to

ld = getTimeStamp((JSONObject) json.get(key));
if (ld != null) return ld;

and change 并改变

getTimeStamp((JSONObject) jarray.get(i));

to

ld = getTimeStamp((JSONObject) jarray.get(i));
if (ld != null) return ld;

Your code returns null because you ignore the results of your recursive invocations. 您的代码返回null因为您忽略了递归调用的结果。 You should store them in a variable, and return it if it's not 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;
            }
        }
    }
}

Note that since you are looking for the first timestamp, the while loop should stop when ld becomes non- null . 请注意,由于您正在寻找第一个时间戳,因此当ld变为非null时, while循环应停止。

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

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