java.lang.String无法转换为java.util.Map

5
我是新手Java程序员,但我不是编程的新手。因此,作为我的第一个项目,我决定为同事创建一个.txt-.csv解析器。我读取.txt文件中的每一行,并将其分成不同的映射集(Map)以用于各个部分、子部分、子子部分和子子部分的内容。然后,将每个Map分配给它上面的Map(下文将详细说明)。我可以成功地打印所有内容,但当我尝试读取时,会出现以下错误:"java.lang.String cannot be cast to java.util.Map"。这个错误仅在代码运行后出现,而不是在编译时或NetBeans IDE中。
我的Maps采用以下形式,其中每个Object都是其下方的Map:(为什么Java不能让这变得容易些呢?关联数组就是我想要的)
(Map)array=<string,Object>
(Map)subarray=<String,Object>
(Map)subsubarray=<String,Object>
(Map)subsubcontents=<String,String>

这可能不是阅读此代码的最有效方法,计划稍后将其转换为递归函数,但这是我从我的项目中复制粘贴的代码。我在发现错误的地方添加了注释。

public static Map<String,Object> array=new HashMap<String,Object>();

/* Code for populating the following Maps and pushing them into array
<String,Object>subarray
<String,Object>subsubarray
<String,String>subsubcontents
*/

Set section=array.entrySet();
Iterator sectionI=section.iterator();
while(sectionI.hasNext()) {
    Map.Entry sectionInfo=(Map.Entry)sectionI.next();
    Map<String,Object> subMap=(Map)sectionInfo.getValue();
    Set subSet=subMap.entrySet();
    Iterator subI=subSet.iterator();
    while(subI.hasNext()) {
            Map.Entry subInfo=(Map.Entry)subI.next();
            Map<String,Object> subsubMap=(Map)subInfo.getValue();
            Set subsubSet=subsubMap.entrySet();
            Iterator subsubI=subsubSet.iterator();
            while(subsubI.hasNext()) {
                    System.out.println("test");
                    Map.Entry subsubInfo=(Map.Entry)subsubI.next();
                    Map<String,Object> subcontentsMap=(Map)subsubInfo.getValue();
/*
The above line seems to be causing the issues.
If you comment out the rest of this loop (below this comment)
the error will still appear. If you comment out the rest of this loop
(including the line above this comment) it disappears.
Power of deduction my dear Watson.
*/
                    Set subcontentsSet=subcontentsMap.entrySet();
                    Iterator keys=subcontentsSet.iterator();
                    while(keys.hasNext()) {
                        Map.Entry keyMap=(Map.Entry)keys.next();
                    }
                    Iterator values=subcontentsSet.iterator();
                    while(values.hasNext()) {
                        Map.Entry valueMap=(Map.Entry)values.next();
                    }
            }
    }
}

非常感谢您的帮助。我已经为此苦苦挣扎了几天了。

你能发布完整的堆栈跟踪吗? - Bhesh Gurung
getStackTrace() 返回 [Ljava.lang.StackTraceElement;@c0a150 - mseancole
3个回答

7

我认为您需要首先清理您的泛型:

Set<Map.Entry<String, Object>> section = array.entrySet();
Iterator<Map.Entry<String, Object>> sectionI = section.iterator();
while (sectionI.hasNext()) {
    Map.Entry<String, Object> sectionInfo = sectionI.next();
    Map<String, Object> subMap = (Map<String, Object>) sectionInfo.getValue(); // is this actually a Map<String, Object>?
    Set<Map.Entry<String, Object>> subSet = subMap.entrySet();
    Iterator<Map.Entry<String, Object>> subI = subSet.iterator();
    while (subI.hasNext()) {
        Map.Entry<String, Object> subInfo = subI.next();
        Map<String, Object> subsubMap = (Map<String, Object>) subInfo.getValue(); // is this actually a Map<String, Object>?
        Set<Map.Entry<String, Object>> subsubSet = subsubMap.entrySet();
        Iterator<Map.Entry<String, Object>> subsubI = subsubSet.iterator();
        while (subsubI.hasNext()) {
            System.out.println("test");
            Map.Entry<String, Object> subsubInfo = subsubI.next();
            Map<String, Object> subcontentsMap = (Map<String, Object>) subsubInfo.getValue(); // somehow a String got in here?
/*
The above line seems to be causing the issues.
If you comment out the rest of this loop (below this comment)
the error will still appear. If you comment out the rest of this loop
(including the line above this comment) it disappears.
Power of deduction my dear Watson.
*/
            Set<Map.Entry<String, Object>> subcontentsSet = subcontentsMap.entrySet();
            Iterator<Map.Entry<String, Object>> keys = subcontentsSet.iterator();
            while (keys.hasNext()) {
                Map.Entry<String, Object> keyMap = keys.next();
            }
            Iterator<Map.Entry<String, Object>> values = subcontentsSet.iterator();
            while (values.hasNext()) {
                Map.Entry<String, Object> valueMap = values.next();
            }
        }
    }
}

那么,你应该更明确地声明数组:
public static Map<String, Map<String, Map<String, Map<String, String>>>> array = new HashMap<String, Map<String, Map<String, Map<String, String>>>>();

这将确保您将正确的对象放入每个映射中。您永远无法在期望 Map<> 的位置放置String 值,因为它不会编译。这将允许您编写以下代码(无需进行转换):

final Set<Map.Entry<String, Map<String, Map<String, Map<String, String>>>>> section = array.entrySet();
final Iterator<Map.Entry<String, Map<String, Map<String, Map<String, String>>>>> sectionI = section.iterator();
while (sectionI.hasNext()) {
    final Entry<String, Map<String, Map<String, Map<String, String>>>> sectionInfo = sectionI.next();
    final Map<String, Map<String, Map<String, String>>> subMap = sectionInfo.getValue();
    final Set<Map.Entry<String, Map<String, Map<String, String>>>> subSet = subMap.entrySet();
    final Iterator<Map.Entry<String, Map<String, Map<String, String>>>> subI = subSet.iterator();
    while (subI.hasNext()) {
        final Map.Entry<String, Map<String, Map<String, String>>> subInfo = subI.next();
        final Map<String, Map<String, String>> subsubMap = subInfo.getValue();
        final Set<Map.Entry<String, Map<String, String>>> subsubSet = subsubMap.entrySet();
        final Iterator<Map.Entry<String, Map<String, String>>> subsubI = subsubSet.iterator();
        while (subsubI.hasNext()) {
            System.out.println("test");
            final Map.Entry<String, Map<String, String>> subsubInfo = subsubI.next();
            final Map<String, String> subcontentsMap = subsubInfo.getValue();
            final Set<Map.Entry<String, String>> subcontentsSet = subcontentsMap.entrySet();
            final Iterator<Map.Entry<String, String>> entries = subcontentsSet.iterator();
            while (entries.hasNext()) {
                final Map.Entry<String, String> entry = entries.next();
            }
        }
    }
}

尽管如此,所有嵌套的泛型看起来很丑。我建议您创建一些对象来表示您的数据。


1
谢谢。虽然这并没有直接解决我的问题,但它让我开始整理我的代码,然后我意识到我忽略了一个 Map。感谢你的建议和帮助,谢谢大家。 - mseancole

4
你可以这样做:
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement element = gson.fromJson (jsonString, JsonElement.class);
JsonObject jsonObj = element.getAsJsonObject();
Map<String,Object> resultMap = new Gson().fromJson(jsonObj, Map.class);

1
异常信息告诉您一切。此调用 subsubInfo.getValue(); 实际上返回的是一个 String,而不是一个Map,因此在创建映射时出现了逻辑错误。
如果您将声明更改为 Map<String, Map> 而不是 Map<String, Object>,编译器将警告您。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接