? ? ? ? 在mapper中寫的語句,結果集中association,采用的一個對象,整個list列表中每個元素有一個對象元素,如果第二個元素中有一個與第一個元素中對象同名的,就會去引用上一個元素的地址,在json前臺解析的時候就不會解析出來相應對象的數據,具體如下,根據網上搜索的原因的例子有:
?
DisableCircularReferenceDetect來禁止循環引用檢測:
JSON.toJSONString(..., SerializerFeature.DisableCircularReferenceDetect)
當進行toJSONString的時候,默認如果重用對象的話,會使用引用的方式進行引用對象。

- "顏色":?[??
- ??????{??
- ????????"$ref":?"$.itemSkuList[0].itemSpecificationList[0]"??
- ??????},???
- ??????{??
- ????????"$ref":?"$.itemSkuList[1].itemSpecificationList[0]"??
- ??????}??
- ????]??
?
循環引用
很多場景中,我們需要序列化的對象中存在循環引用,在許多的json庫中,這會導致stackoverflow。在功能強大的fastjson中,你不需要擔心這個問題。例如:

- A?a?=?new?A();??
- B?b?=?new?B(a);??
- a.setB(b);??
- String?text?=?JSON.toJSONString(a);?//{"b":{"a":{"$ref":".."}}}??
- A?a1?=?JSON.parseObject(text,?A.class);??
- Assert.assertTrue(a1?==?a1.getB().getA());??
?
引用是通過"$ref"來表示的
引用 | 描述 |
---|---|
"$ref":".." | 上一級 |
"$ref":"@" | 當前對象,也就是自引用 |
"$ref":"$" | 根對象 |
"$ref":"$.children.0" | 基于路徑的引用,相當于 root.getChildren().get(0) |
對于本博主的項目,其解決方案是,1).將結果集中association的類型寫成map
2).在最后取得包含了對象的list列表后,將該列表轉換為json格式,本博主代碼
PageInfo<Map<String,Object>> pageInfo = rentService.getRentOrderList(Integer.valueOf(pageNumber), Integer.valueOf(pageSize), paramMap);Map<String,Object> data = new HashMap<String,Object>();data.put("orderList",pageInfo.getList());data.put("total", pageInfo.getTotal());Object objData = JSONObject.toJSON(data);
?