如何使用for循环使用keyset迭代hashmap

时间:2016-02-28 07:07:51

标签: java json

我正在尝试使用keyset检索Employee和department表。我在hashmap中有jsonObject。如何使用for循环迭代Employee.employeeId。任何人都可以帮助我...

我的simple.json:

{
    "routings": {
        "routing1": {
            "targetCollection-name": "EmployeeData",
            "sourcetables-data": {
                "Employee": [{
                    "name": "employeeId",
                    "sourceDataType": "number",
                    "targetField": "employeeId",
                    "targetDataType": "double"
                }, {
                    "name": "firstName",
                    "sourceDataType": "varchar2",
                    "targetField": "firstName",
                    "targetDataType": "string"
                }, {
                    "name": "lastName",
                    " sourceDataType": "varchar2",
                    "targetField": "lastName",
                    "targetDataType": "string"
                }, {
                    "name": "contactNumber1",
                    "sourceDataType": "number",
                    "targetField": "contactNumbers",
                    "targetDataType": "array"
                }],
                "department": [{
                    "name": "departmentNumber",
                    "sourceDataType": "number",
                    "targetField": "departmentNumber",
                    "targetDataType": "double"
                }, {
                    "name": "departmentType",
                    "sourceDataType": "number",
                    "targetField": "departmentType",
                    "targetDataType": "double"
                }, {
                    "name": "startDate",
                    "sourceDataType": "timestamp",
                    "targetField": "startDate",
                    "targetDataType": "date"
                }],
                "foriegnkey": [{
                    "parentTable": "Employee",
                    "parentkey": "employeeId",
                    "childTable": "department",
                    "childKey": "empId"
                }]
            }
        }
    }

}

我的代码:

public class DataParse {

    public static void main(String a[]){

        String FILEPATH = "C:\\Users\\BNSK\\Downloads\\simple.json"; 

        Map<String,Object> resultMap = new HashMap<String,Object>();
        ObjectMapper mapperObj = new ObjectMapper();

        System.out.println("Input Json: "+FILEPATH);
        try {
            resultMap = mapperObj.readValue(new File(FILEPATH),
                            new TypeReference<HashMap<String,Object>>(){});
            System.out.println("Output Map: "+resultMap);


for (String key : resultMap.keySet()) {
             Object routings= resultMap.get(key);
           System.out.println(routings);

        }



        } catch (IOException e) {

            e.printStackTrace();
        }
    }

2 个答案:

答案 0 :(得分:1)

Elliot是对的,我还想在Java 8中添加你可以使用流(和lambda表达式)轻松迭代Map键

resultMap.keySet().stream().forEach(key -> {
    // do your thing here 
});

答案 1 :(得分:0)

例如:

    Map routings = (Map)resultMap.get("routings");

    // lets iterate every routing in routings
    for(Object routingKey : routings.keySet()){

        Map routing = (Map)routings.get(routingKey);
        Map tableData = (Map)routing.get("sourcetables-data");

        List employeeList = (List)tableData.get("Employee");
        //lets print all names of Employee
        for(Object item : employeeList){
           Map field = (Map)item;
           Object name = field.get("name");
           Object value = field.get(name);
           System.out.println(name+"="+value);
        }
    }