通过深度HashMap递归迭代

Iterate recursively through deep HashMap(通过深度HashMap递归迭代)
本文介绍了通过深度HashMap递归迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个类似于以下内容的 JSON 字符串:

I have a JSON string that resembles the following:

{
    "foo" : "bar",
    "id" : 1,
    "children":[
        {
            "some" : "string",
            "id" : 2,
            children : []
        },
        {
            "some" : "string",
            "id" : 2,
            children : []
        }
    ]
}

我对此字符串进行 JSON 解析,然后将所有对象转换为 HashMap,将所有数组转换为 HashMap[].我的问题是我需要一个递归函数来遍历 Java 中这个 JSON 结构的所有节点.我怎样才能做到这一点?我在想这样的事情:

I do a JSON parse of this string, and that turns all objects into HashMaps and all arrays into HashMap[]s. My problem is I need a single recursive function to iterate through all nodes of this JSON structure in Java. How can I do this? I was thinking something like:

public HashMap findNode(boolean isArray, HashMap map, HashMap[] array){
    //array stuff
    if(isArray){
        for(int i=0; i<array.length(); i++){
            Object value = array[i];
            if(value instanceof String)
                System.out.println("value = "+value);
            else if(value instanceof HashMap)
                findNode(false, value, null);
            else if(value instanceof HashMap[])
                findNode(true, null, value);
        }
    //hashmap stuff
    }else{
        for(HashMap.Entry<String, Object> entry : map.entrySet()){
            Object value = entry.getValue();
            if(value instanceof String)
                System.out.println("value = "+value);
            else if(value instanceof HashMap)
                findNode(false, value, null);
            else if(value instanceof HashMap[])
                findNode(true, null, value);
        }
    }
}

推荐答案

假设你的一个数组里面只能有 Maps(而不是其他数组):

Assuming you an array can only have Maps inside (and not other arrays):

public void findNode(HashMap map) {
    for(HashMap.Entry<String, Object> entry : map.entrySet()){
        Object value = entry.getValue();
        if(value instanceof String)
            System.out.println("value = "+value);
        else if(value instanceof HashMap)
            findNode(value);
        else if(value instanceof HashMap[])
            for(int i=0; i<array.length(); i++){
                findNode(array[i]);
    }
}

或者如果你可以使用 3 个函数,你可以让它变得更简单

Or you can make it even simpler if you can use 3 functions

public void findNode(HashMap map) {
    for(HashMap.Entry<String, Object> entry : map.entrySet()){
        findNode(entry.getValue());
    }
}

public void findNode(String value) {
    System.out.println("value = "+value);
}

public void findNode(HashMap[] value) {
    for(int i=0; i<array.length(); i++){
        findNode(array[i]);
    }
}

这篇关于通过深度HashMap递归迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)