Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么break不结束while循环?_Java_Json - Fatal编程技术网

Java 为什么break不结束while循环?

Java 为什么break不结束while循环?,java,json,Java,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":"Vo

我有一些建议:

{"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;
    }
private static LocalDate getTimeStamp(JSONObject json)抛出IOException{
迭代器keys=json.keys();
LocalDate ld=null;
//递归迭代JSON中的所有键,直到当前键
//时间戳
while(keys.hasNext()){
字符串键=(字符串)键。下一步();
if(key.equals(“时间戳”)){
//时区不重要,因为我们只想提取日期
DateTimeFormatter formatter=DateTimeFormatter.of模式(
“yyyy-MM-dd'T'HH:MM:ss'Z'”,Locale.ENGLISH);
ld=LocalDate.parse(json.get(key.toString(),格式化程序);
系统输出打印项次(ld);
打破
}
//如果当前值是另一个JSON对象,则调用setTimeStamp
//以值作为参数
else if(json.get(key)JSONObject实例){
getTimeStamp((JSONObject)json.get(key));
}
//处理当前值为JSON数组
else if(json.get(key)instanceof JSONArray){
JSONArray jarray=(JSONArray)json.get(key);
for(int i=0;i
我试着调试它,但我不明白为什么在调用
break
后调用该方法。它返回null而不是时间戳,尽管它到达了代码的这一部分。

键嵌套在JSON中,因此需要对方法进行一些递归调用才能到达它

进行递归调用时,应使用这些调用返回的值

改变

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

改变

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


您的代码返回
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(keys.hasNext()&&ld==null){
...
else if(json.get(key)JSONObject实例){
ld=getTimeStamp((JSONObject)json.get(key));
}
//处理当前值为JSON数组
else if(json.get(key)instanceof JSONArray){
JSONArray jarray=(JSONArray)json.get(key);
for(int i=0;i

请注意,由于您正在查找第一个时间戳,
while
循环应在
ld
变为非
null

调用
break
后调用什么方法时停止。程序正在递归工作。我怀疑类似于。它返回
null
,因为
“continue”
不等于
“timestamp”
(并且忽略递归调用的结果)。如果不中断,可能意味着“if(key.equals(“timestamp”){”不工作。key不等于timestamp?
ld = getTimeStamp((JSONObject) jarray.get(i));
if (ld != null) return ld;
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;
            }
        }
    }
}