美文网首页
Java Reflect与JSONArray的一个应用场景

Java Reflect与JSONArray的一个应用场景

作者: writeanewworld | 来源:发表于2018-06-08 18:13 被阅读0次

    1.应用


    image.png

    2.数据库结构


    image.png

    3.我想要的json格式


    image.png

    4.场景:
    经理提出要求,给他把图一的页面渲染好,嗯~

    image.png

    我的做法:直接在JsonTemplateParams实体类中声明一个ItemSku实体类,通过value(两张表的关联字段)调用查询接口,然后setItemSku就拼装完成了。

    可是经理看了之后不允许使用这种方法。。。(模板实体类比较特殊)

    5.Map + 反射
    那好吧,这样的话使用一个Map来拼装,反正用model.addAttribute("",);传到页面 前端thymleaf模板会识别的。

    map格式:List<Map<String, Object>> array = new ArrayList<String,Object>();
    已经拿到了JsonTemplatesPutsParams集合:

    List<JsonTemplatesPutsParams> jsonTemplatesPutsParams = 
    getParams(pageId,claName+".itemNo");
    

    写法如下:

        List<Map<String, Object>> array = new ArrayList<>();
        Class<JsonTemplatesPutsParams> clazz = JsonTemplatesPutsParams.class;
        for (JsonTemplatesPutsParams jsonTemplate: jsonTemplatesPutsParams) {
            Map<String, Object> map = new HashMap<>();
            for (Field f: clazz.getDeclaredFields()) {
                f.setAccessible(true);
                try {
                    map.put(f.getName(), f.get(jsonTemplate));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
          ItemSku itemSku = itemResponsitoryAdmin.selectItemSkuList(jsonTemplate.getValue()).get(0);
          itemSku.setImage(COSUtil.getSignatureUrl(itemSku.getImage()));
          map.put("itemSku", itemSku);
          array.add(map);
        }
    

    有点臃肿,总之不是很满意,查询了一下JSON的api,感觉JSONArray写一下会简单很多。

    6.JSONArray

        JSONArray array = (JSONArray)JSON.toJSON(jsonTemplatesPutsParams);
        for (int i=0, len=array.size(); i<len; ++i) {
            JSONObject obj = (JSONObject) array.get(i);
            JsonTemplatesPutsParams jsonTemplate = jsonTemplatesPutsParams.get(i);
            ItemSku itemSku = itemResponsitoryAdmin.selectItemSkuList(jsonTemplate.getValue()).get(0);
            itemSku.setImage(COSUtil.getSignatureUrl(itemSku.getImage()));
            obj.put("itemSku", JSON.toJSON(itemSku));
        }
    

    这样的话感觉精简了许多.
    model.addAttribute("item",array);

    7.html代码

    <table class="table table-bordered">
                          <tbody>
                          <tr >
                              <th style="width: 10px">#</th>
                              <th style="text-align:center">商品规格号</th>
                              <th style="text-align:center">商品规格编号</th>
                              <th style="text-align:center">商品名称</th>
                              <th style="text-align:center">商品图片</th>
                              <th style="text-align:center">排序号</th>
                              <th style="width: 40px;text-align:center">操作</th>
                          </tr>
                          <tr style="text-align:center" th:each="item,row : ${items}">
                              <td th:text="${row.count}+'.'"></td>
                              <td th:text="${item.value}"></td>
                              <td th:text="${item.itemSku.sku}"></td>
                              <td th:text="${item.itemSku.name}"></td>
                              <td>
                                 <img th:attr="src=${item.itemSku.image}" alt="商品规格图片" style="width:35px;height:35px;">
                              </td>
                              <td th:text="${item.sort}"></td>
                              <td>
                                  <form th:action="'deleteItem/'+${item.id}">
                                      <input type="hidden"/>
                                      <button class="btn btn-primary">删除</button>
                                  </form>
                              </td>
                          </tr>
                          </tbody>
    </table>

    相关文章

      网友评论

          本文标题:Java Reflect与JSONArray的一个应用场景

          本文链接:https://www.haomeiwen.com/subject/axgasftx.html