简体   繁体   中英

Why does break not end the while loop?

I have some 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:

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 . It returns null instead of the timestamp although it reaches that part of the code.

The "timestamp" key is nested inside your JSON, so it takes a few recursive calls of your method to get to it.

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. You should store them in a variable, and return it if it's not 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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