【问题标题】:How to create a Java map of strings to a complex object built from multiple data rows如何创建从多个数据行构建的复杂对象的 Java 字符串映射
【发布时间】:2019-11-14 19:28:46
【问题描述】:

我运行查询并从数据库中取回以下数据。

painter_id  color_id    color   used 
1           100         blue    t
1           101         green   t
1           102         red     f
1           103         black   f
1           104         yellow  f
2           110         violet  t
2           111         cyan    t
2           112         brown   t
2           113         white   f
2           114         orange  f

我需要帮助构建以下输出;

{
    "colors": {
        "1": {
            "used": {
                "results": [
                    "blue",
                    "green"
                ]
            },
            "notUsed": {
                "results": [
                    "red",
                    "black",
                    "yellow"
                ]
            }
        },
        "2": {
            "used": {
                "results": [
                    "violet",
                    "cyan",
                    "brown"
                ]
            },
            "notUsed": {
                "results": [
                    "white",
                    "orange"
                ]
            }
        }
    }
}

输出的swagger定义是;

ColorResponse:
  type: object
  properties:
    colors:
      type: object
      additionalProperties:
        $ref: "#/definitions/ColorUsages"

ColorUsages:
  type: object
  properties:
    used:
      $ref: "#/definitions/ColorUsage"
    notUsed:
      $ref: "#/definitions/ColorUsage"

ColorUsage:
  type:  object
  properties:
    count:
      type: integer
      format: int32
      description: How many colors are there
    results:
      type: array
      items:
        type: string
        description: The color name     

在哪里;

Map<String, ColorUsage> colors = new HashMap<String, ColorUsages>();
ColorUsage used;
ColorUsage notUsed;
Integer count;
List<String> results = new ArrayList<>();

假设您的查询结果可以使用;

data.forEach( row -> row.painter_id(),  row.color_id(), row.color(), row.used())        

还假设您可以使用以下内容;

Map<String, ColorUsages> colors = new HashMap<>();

ColorUsage.Builder usedColorUsage = ColorUsage.builder();
ColorUsage.Builder notUsedColorUsage = ColorUsage.builder();

usedColorUsage.addResultsItem(row.color());

ColorUsages.Builder colorUsagesBuilder = ColorUsages.builder();

ColorUsages colorUsages = colorUsagesBuilder
                        .used(usedColorUsage.build())
                        .notUsed(notUsedColorUsage.build())
                        .build();

colors.put(row.painter_id(), colorUsages);

我目前正在获取数据,对其进行一些小的更新并将其存储在 TreeBasedTable 中。然后我遍历颜色并需要为画家构建 usedColorUsage 和 notUsedColorUsage 对象。然后我需要将 usedColorUsage 和 notUsedColorUsage 添加到 colorUsages。最后,我需要将painter_id和colorUsages的映射组合添加到颜色中。

【问题讨论】:

    标签: java stream swagger


    【解决方案1】:

    使用 org.json.simple.JSONArray 和 org.json.simple.JSONObject 以及 Map 让您的工作变得轻松。

    public JSONObject buildOutPut(List<TableData> dataList) {
    
            JSONObject finalObject = new JSONObject();
    
            Map<String, JSONObject> map = new HashMap<String, JSONObject>();
    
            for(TableData oneRow: dataList) {
                if(map.containsKey(oneRow.getPainterId())) {
    
                    JSONObject subObject =  map.get(oneRow.getPainterId());
    
    
                    if(oneRow.getUsed().equalIgnoreCase("t")) {
                        JSONObject usedResultsObj  = (JSONObject) subObject.get("used");
                        JSONArray usedResults  = (JSONArray) usedResultsObj.get("results");
                        usedResults.add(oneRow.getColor());
    
                        usedResultsObj.put("results", usedResults);
                        subObject.put("used", usedResultsObj);
                        map.put(oneRow.getPainterId(), subObject);
    
                    } else if(oneRow.getUsed().equalIgnoreCase("f")) {
                        JSONObject notUsedResultsObj  = (JSONObject) subObject.get("notUsed");
                        JSONArray notUsedResults  = (JSONArray) notUsedResultsObj.get("results");
                        notUsedResults.add(oneRow.getColor());
    
                        notUsedResultsObj.put("results", notUsedResults);
                        subObject.put("notUsed", notUsedResults);
                        map.put(oneRow.getPainterId(), subObject);
    
                    }
    
    
                } else {
                    JSONObject subObject = new JSONObject();
    
                    JSONObject usedResultsObj = new JSONObject();
                    JSONObject notUsedResultsObj = new JSONObject();
    
                    JSONArray usedResults = new JSONArray();
                    JSONArray notUsedResults = new JSONArray();
    
                    if(oneRow.getUsed().equalIgnoreCase("t")) {
                        usedResults.add(oneRow.getColor());
                    } else if(oneRow.getUsed().equalIgnoreCase("f")) {
                        notUsedResults.add(oneRow.getColor());
                    }
    
                    usedResultsObj.put("results", usedResults);
                    notUsedResultsObj.put("results", notUsedResults);
                    subObject.put("used", usedResultsObj);
                    subObject.put("notUsed", notUsedResultsObj);
    
                    map.put(oneRow.getPainterId(), subObject);
                }
            }
    
            finalObject.put("colors", map);
    
            return finalObject;
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-22
      • 1970-01-01
      • 2019-03-20
      • 2011-04-30
      相关资源
      最近更新 更多