在JSON中打印hashmap内容

时间:2013-11-04 04:35:26

标签: java

我有一个hashmap,我在其中存储一个人拥有的汽车名单。

Map<String,List<String>> personCarMap = new HashMap<String,List<String>>();

键是人员ID,值是车名列表。在价值中,我还想存储购买日期。所有这些都是从数据库中提取的。如何在数据库中有两个不同列的键映射?

这是我到目前为止的代码。

public static JSONObject fetchPersonCarInfo(String person) throws SQLException
{
    Map<String,List<String>> personCarMap = new HashMap<String,List<String>>();
    JSONObject jObj = new JSONObject();
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet rs = null;        
    try {
        connection = DriverManager.getConnection("jdbc:myDriver:myDatabase",username,password);
        statement = connection.prepareStatement("select personId,car,purchase_date from carz where person = ?");
        rs = statement.executeQuery();          
        while(rs.next()) {  
            List<String> carIds;
            if(personCarMap.containsKey(rs.getString(carId))) {
                carIds = personCarMap.get(rs.getString(personId));
            } else {
                carIds = new ArrayList<String>();
            }               
            carIds.add(rs.getString(carId));
            carIds.add(rs.getTimestamp(purchase_date).toString()); //This is where I have a doubt.
            personCarMap.put(rs.getString(carId),carIds);           
        }
        for(Map.Entry<String, List<String>> personCarMapEntry:personCarMap.entrySet())
        {
            jObj.put("personID", personCarMapEntry.getKey());
            jObj.put("carID", personCarMapEntry.getValue());                
        }
    } finally{
        DatabaseFunctions.closeDBResources(null, statement, rs);
    }       
    return jObj;
}   
}

我希望我的JSON对象显示{personId,[carId,purchaseate]}。请建议我如何让JSON阵列打印出购买日期?我看到输出明显为personId和carId,但我无法弄清楚如何添加购买日期,所以需要你的专家建议。

2 个答案:

答案 0 :(得分:2)

您需要使用具有两个属性carId和purchaseDate的自定义对象列表。

Map<String,List<ClassNameOfCustomObject>> personCarMap = new HashMap<String,List<ClassNameOfCustomObject>>();

这样您就可以将purchaseDate与carId一起存储在personCarMap中。

答案 1 :(得分:1)

我建议直接使用JSONObject。它有一些方便的方法。

public static JSONObject fetchPersonCarInfo(String person) throws SQLException
{
    JSONObject jObj = new JSONObject();
    PreparedStatement statement = null;
    ResultSet rs = null;        
    try {
        Connection connection = DriverManager.getConnection("jdbc:myDriver:myDatabase",username,password);
        statement = connection.prepareStatement("SELECT personId, car, purchase_date FROM carz WHERE person = ?");
        statement.setString(1, person);
        rs = statement.executeQuery();
        while(rs.next()) {
            jObject.put("personId", rs.getString("personId");

            JSONObject purchase = new JSONObject();
            // there is no column with name carId in above query.
            purchase.put("carId", rs.getString("car");
            purchase.put("purchasedate", rs.getTimestamp("purchase_date").toString());  
            jObject.append("purchases", purchase);
        }
    } finally{
        DatabaseFunctions.closeDBResources(null, statement, rs);
    }       
    return jObj;
}

这将产生这样的JSON:

{
    "personId": "ABC123",
    "purchases": [
        {"carId": "abc123", "purchasedate": "2013-01-01 00:00:00"},
        {"carId": "def456", "purchasedate": "2013-01-02 00:00:00"}
    ]
}

有点难以理解你希望你的JSON看起来像什么,但我认为就是这样。无论如何,这个JSON结构对于您的数据来说是一个很好的结构。