1

I get a List like this, one Object per row holding plain strings:

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

I want to return that as JSON in the form of:

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

I tried using a weird

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

but to be honest, I don't know how to set this up correctly. When I iterate over the list and write to the HashMap, Property and Value always get overwritten, so my result looks like this:

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

I'm fairly new to Java and don't know how the handle this kind of nested Maps.

I mean, asking for being pampered is a bit embarrassing, but maybe someone can show me how to do this in a correct way.

Edit: here how I add values, I haven't pasted this part of the code as I don't know HOW to check and decide what to do.

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

データ構造が3深さであるのに対し、ハッシュマップは2深さで使用しています。それはインセプションのようなものです:「私たちはもっと深く行く必要があります!」

{ 
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 に答える