通过深入的HashMap迭代递归 [英] Iterate recursively through deep HashMap

查看:284
本文介绍了通过深入的HashMap迭代递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于下面的JSON字符串:

  {
    富:酒吧,
    标识:1,
    孩子:
        {
            一些:串,
            ID:2,
            孩子:[]
        },
        {
            一些:串,
            ID:2,
            孩子:[]
        }
    ]
}

我做这个字符串的JSON解析,并关闭所有的物体进入HashMaps这样,所有的阵列进行的HashMap []秒。我的问题是,我需要一个递归函数通过Java的这种JSON结构的所有节点进行迭代。我怎样才能做到这一点?我的想法是这样的:

 公开的HashMap findNode(布尔IsArray的,HashMap的地图,HashMap的[]数组){
    //数组的东西
    如果(IsArray的){
        的for(int i = 0; I< array.length();我++){
            对象值=阵列[我]
            如果(价值的instanceof字符串)
                的System.out.println(值=+值);
            否则,如果(价值的instanceof的HashMap)
                findNode(假的,值,NULL);
            否则,如果(价值的instanceof HashMap的[])
                findNode(真,空,价值);
        }
    // HashMap的东西
    }其他{
        为(HashMap.Entry&下;串,对象>项:map.entrySet()){
            对象值= entry.getValue();
            如果(价值的instanceof字符串)
                的System.out.println(值=+值);
            否则,如果(价值的instanceof的HashMap)
                findNode(假的,值,NULL);
            否则,如果(价值的instanceof HashMap的[])
                findNode(真,空,价值);
        }
    }
}


解决方案

假设你的数组只能有地图内(而不是其他阵列):

 公共无效findNode(HashMap的地图){
    为(HashMap.Entry&下;串,对象>项:map.entrySet()){
        对象值= entry.getValue();
        如果(价值的instanceof字符串)
            的System.out.println(值=+值);
        否则,如果(价值的instanceof的HashMap)
            findNode(值);
        否则,如果(价值的instanceof HashMap的[])
            的for(int i = 0; I< array.length();我++){
                findNode(数组[我]);
    }
}

或者你也可以让它更简单,如果你可以使用3种功能

 公共无效findNode(HashMap的地图){
    为(HashMap.Entry&下;串,对象>项:map.entrySet()){
        findNode(entry.getValue());
    }
}公共无效findNode(字符串值){
    的System.out.println(值=+值);
}公共无效findNode(HashMap的[]值){
    的for(int i = 0; I< array.length();我++){
        findNode(数组[我]);
    }
}

I have a JSON string that resembles the following:

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

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);
        }
    }
}

解决方案

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]);
    }
}

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迭代递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆