1

我得到一个这样的列表,每行一个对象包含纯字符串:

Vehicle, Name, Property, Value
------------------------------
Car, VW, Tires, 4
Car, VW, Doors, 4
Car, Porsche, Tires, 4
Car, Porsche, Doors, 2
Car, Porsche, Color, Red
Plane, A340, Tires, 12
Plane, A340, Color, White
Plane, A750, Doors, 6
Forklift, ABC345, Color, Orange
... and so on

我想以 JSON 的形式返回它:

{ 
"Car" : {
     "VW":{ "Tires" : "4", "Doors": "4"},
     "Porsche":{ "Tires" : "4", "Doors": "2" }
     },
"Plane":{
     "A340":{ "Tires" : "12", "Color" : "White" },
     "A750":{ "Doors" : "6" }
     },
"Forklift" : {
     "ABC345": { "Color" : "Orange" }
     }
}

我尝试使用一个奇怪的

HashMap<String, HashMap<String, HashMap<String, String>>>

但老实说,我不知道如何正确设置它。当我遍历列表并写入 HashMap 时,Property 和 Value 总是被覆盖,所以我的结果如下所示:

{ 
"Car" : {
     "Porsche":{ "Tires" : "4" }
     },
"Plane":{
     "A750":{ "Doors" : "6" }
     },
"Forklift" : {
     "ABC345": { "Color" : "Orange" }
     }
}

我对Java相当陌生,不知道如何处理这种嵌套的地图。

我的意思是,要求被宠爱有点尴尬,但也许有人可以告诉我如何以正确的方式做到这一点。

编辑:在这里我如何添加值,我没有粘贴这部分代码,因为我不知道如何检查和决定做什么。

HashMap<String, HashMap<String, HashMap<String, String>>> vehiclesData = new HashMap<String, HashMap<String, HashMap<String, String>>>();

for( VehicleReportRow vehicleReportRow : unpreparedRows ){
    String vehicle = vehicleReportRow.getVehicle();
    String name = vehicleReportRow.getName();
    String property = vehicleReportRow.getProperty();
    String value = vehicleReportRow.getValue();

    //the most inner dataset
    HashMap<String,String> propAndVal = new HashMap<String, String>();
   propAndVal.put(property, value);

    //the middle dataset            
    HashMap<String, HashMap<String,String>> nameToProps = new HashMap<String, HashMap<String,String>>();
    nameToProps.put(name, propAndVal);

    //and finally the outer dataset
    vehiclesData.put(vehicle, nameToProps);
}
4

2 回答 2

1

您正在使用两层深度的哈希图,而您的数据结构是三层深度。就像《盗梦空间》:“我们需要更深入!”

{ 
1: "Car" : {
2:     "VW":{ 
3:         "Tires" : "4", "Doors": "4"
       },
       "Porsche":{ 
           "Tires" : "4", "Doors": "2" 
       }
   },
}
于 2012-08-29T16:17:33.760 回答
1

现在看到您的代码后,问题正如我在评论中所暗示的那样。例如,您每次在循环中创建一个新的nameToProps和一个新propAndVal的,而不是检查是否存在您应该添加的现有的。您需要一些检查,例如:

for( VehicleReportRow vehicleReportRow : unpreparedRows ){
    String vehicle = vehicleReportRow.getVehicle();
    String name = vehicleReportRow.getName();
    String property = vehicleReportRow.getProperty();
    String value = vehicleReportRow.getValue();

    // check if we have an outermost entry for this vehicle type and if not then
    // create one and store it in vehiclesData so that next time we can get the same
    // map for this vehicle type
    HashMap<String, HashMap<String,String>> nameToProps = vehiclesData.get(vehicle);;
    if (nameToProps == null) {
        nameToProps = new HashMap<String, HashMap<String,String>>();
        vehiclesData.put(vehicle, nameToProps);
    }

    // similarly, check if we already have a props to values map for this name
    // and create and store one if not
    HashMap<String,String> propAndVal = nameToProps.get(name);
    if (propAndVal == null) {
        propAndVal = new HashMap<String, String>();
        nameToProps.put(name, propAndVal);
    }

    // store the property and value
    propAndVal.put(property, value);
}

如果其中任何一个需要进一步解释,请告诉我。

于 2012-08-29T17:58:30.423 回答