You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从嵌套HashMap中取值?针对Map<String,Map1<String,Object>>获取marks值

How to Extract the Value from the marks Key in Your Nested Map

Hey there! Let's walk through how to get the numerical value under the marks key step by step, using your example data. First, let's recap your structure:

Your sample data: {"name": "mm", "language known": "English", "marks": {"english": ["88"]}}

This is a nested structure: an outer Map, where the marks key maps to another inner Map, and that inner Map's english key maps to a List containing the value we want (88).

General Approach

The core idea is to traverse the nested layers one by one, while adding checks to avoid errors (like missing keys or incorrect data types). Here are the key steps:

  1. Access the inner Map associated with the marks key in the outer Map.
  2. From that inner Map, access the List associated with the english key.
  3. Extract the first element from the List (since your example has only one entry).

Example in Java

If you're working with Java's Map interface, here's a safe implementation with error checks:

// Initialize your nested Map (matching your sample data)
Map<String, Object> outerMap = new HashMap<>();
Map<String, Object> marksInnerMap = new HashMap<>();
List<String> englishMarksList = new ArrayList<>();
englishMarksList.add("88");

marksInnerMap.put("english", englishMarksList);
outerMap.put("name", "mm");
outerMap.put("language known", "English");
outerMap.put("marks", marksInnerMap);

// Extract the mark value
if (outerMap.containsKey("marks") && outerMap.get("marks") instanceof Map) {
    Map<String, Object> innerMarksMap = (Map<String, Object>) outerMap.get("marks");
    
    if (innerMarksMap.containsKey("english") && innerMarksMap.get("english") instanceof List) {
        List<?> marksList = (List<?>) innerMarksMap.get("english");
        
        if (!marksList.isEmpty()) {
            Object rawMark = marksList.get(0);
            System.out.println("Raw English mark: " + rawMark); // Outputs "88"
            
            // Convert to integer if needed
            if (rawMark instanceof String) {
                int englishMark = Integer.parseInt((String) rawMark);
                System.out.println("English mark as integer: " + englishMark); // Outputs 88
            }
        }
    }
}

Example in Python

If you're using Python dictionaries (which act like Maps), the code is more concise but still includes safety checks:

# Your sample data as a Python dictionary
outer_dict = {
    "name": "mm",
    "language known": "English",
    "marks": {"english": ["88"]}
}

# Extract the mark value
if "marks" in outer_dict:
    inner_marks_dict = outer_dict["marks"]
    
    if "english" in inner_marks_dict:
        marks_list = inner_marks_dict["english"]
        
        if marks_list:  # Check if the list isn't empty
            raw_mark = marks_list[0]
            print(f"Raw English mark: {raw_mark}") # Outputs "88"
            
            # Convert to integer
            english_mark = int(raw_mark)
            print(f"English mark as integer: {english_mark}") # Outputs 88

Why the Checks?

Adding checks for existing keys and correct data types prevents runtime errors (like NullPointerException in Java or KeyError in Python) if the Map structure doesn't match your expectations (e.g., missing keys, or a non-List value for english).

内容的提问来源于stack exchange,提问作者Nivetha G

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:08:42