简体   繁体   English

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

[英]java.lang.String cannot be cast to java.util.Map

I'm new to Java, but not new to programming, so as my first project I decided to create a .txt-.csv parser for someone at work. 我是Java的新手,但不是编程的新手,所以作为我的第一个项目,我决定为工作中的某个人创建一个.txt-.csv解析器。 I read each line in the .txt file and separate it into separate Maps for sections, subsections, subsubsections, and the subsubsections' contents. 我读取了.txt文件中的每一行,并将其分成单独的地图,用于部分,子部分,子部分和子部分的内容。 Each Map is then assigned to the Map above it (more on this below). 然后将每个地图分配到其上方的地图(下面将详细介绍)。 I print everything to it just fine, but when I try to read it I get the following error: "java.lang.String cannot be cast to java.util.Map" . 我打印的一切都很好,但是当我尝试读取它时,我收到以下错误: "java.lang.String cannot be cast to java.util.Map" The error only appears after the code is run, not while compiling, nor in NetBeans IDE. 该错误仅在代码运行后出现,而不是在编译时出现,也不出现在NetBeans IDE中。

My Maps are in the following form with each Object being the Map below it: (Why can't Java make this easy -_- Associative Arrays are all I want) 我的地图是以下形式,每个对象都是它下面的地图:(为什么Java不能使这个简单的-_-关联数组都是我想要的)

(Map)array=<string,Object>
(Map)subarray=<String,Object>
(Map)subsubarray=<String,Object>
(Map)subsubcontents=<String,String>

May not be the most efficient way to read this, plan on converting this to recursive function later, but here is my code, copy-pasted from my project. 可能不是最有效的阅读方法,计划稍后将其转换为递归函数,但这是我的代码,从我的项目中复制粘贴。 I put comments at where I've found the error to be. 我把评论放在我发现错误的地方。

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

Any help would be much appreciated. 任何帮助将非常感激。 I've been struggling with this for a couple of days now. 我几天都在努力解决这个问题。

I think you need to clean up your generics to start with: 我认为你需要清理你的泛型开头:

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

Then, you should be more explicit with your declaration of array : 然后,您应该更明确地声明array

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

This would ensure that you are putting the correct objects into each of the maps. 这将确保您将正确的对象放入每个地图中。 You will never be able to put a String value where a Map<> is expected because it will not compile. 您永远无法put String值放在期望Map<>位置,因为它不会编译。 This will allow you to write the following code (without needing casts): 这将允许您编写以下代码(无需强制转换):

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

All that being said, all of those nested generics look ugly. 总而言之,所有这些嵌套的泛型看起来都很难看。 I'd recommend you create some objects to represent your data. 我建议你创建一些对象来表示你的数据。

You can do this : 你可以这样做 :

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

The exception tells you everything. 例外情况告诉你一切。 This call subsubInfo.getValue(); 这个调用subsubInfo.getValue(); is actually returning a String , not a Map , so you have a logical error when creating your maps. 实际上是返回一个String而不是Map ,因此在创建地图时会出现逻辑错误。

The compiler will warn you about this if you change your declarations to Map<String, Map> instead of Map<String, Object> 如果您将声明更改为Map<String, Map>而不是Map<String, Object>编译器将向您发出警告

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:java.lang.String 无法转换为 java.util.Map - java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map 无法将java.lang.String强制转换为java.util.Map $ Entry,并且分页缺少必需的属性 - java.lang.String cannot be cast to java.util.Map$Entry and pagination missing required attribute 无法将java.lang.String强制转换为java.util.Map $ Entry - java.lang.String cannot be cast to java.util.Map$Entry Solr CloudSolrClient:原因:java.lang.ClassCastException:java.lang.String无法转换为java.util.Map - Solr CloudSolrClient : Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map java neo4j用cypher创建节点:java.lang.String不能强制转换为java.util.Map - java neo4j creating node with cypher: java.lang.String cannot be cast to java.util.Map 预期的数组类型; 找到:'java.util.map<java.lang.string,java.lang.string> '</java.lang.string,java.lang.string> - Array type expected; found: 'java.util.map<java.lang.String,java.lang.String>' cassandra datastac Codec not found varchar &lt;-&gt; java.util.Map<java.lang.string, java.lang.string></java.lang.string,> - cassandra datastac Codec not found varchar <-> java.util.Map<java.lang.String, java.lang.String> 未找到请求操作的编解码器:[冻结<ynapanalyticsteam.ynapnestedmap> &lt;-&gt; java.util.Map<java.lang.String, java.lang.String> ] - Codec not found for requested operation: [frozen<ynapanalyticsteam.ynapnestedmap> <-> java.util.Map<java.lang.String, java.lang.String>] java.lang.ClassCastException: java.util.HashMap$EntrySet 不能转换为 java.util.Map$Entry - java.lang.ClassCastException: java.util.HashMap$EntrySet cannot be cast to java.util.Map$Entry java.lang.ClassCastException:无法将java.lang.Boolean强制转换为java.util.Map - java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.util.Map
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM