我正在編寫一個使用java和spring mvc框架的web應用程序。 (我想指定一些點數,然後谷歌地圖將其繪製在地圖上)。如何將對象列表表示爲一個json對象數組
在我的java控制器中,我建立了對象列表,然後通過ModelAndView將它返回給我的客戶端代碼。
<h1>List of Geo-Location points: </h1>
<c:forEach items="${mapcontent}" var="point">
${point.latitude} ${point.longitude} ${point.message}
<br />
</c:forEach>
我對結果很滿意,因爲它打印出我所有的相關結果:
在我看來,我然後按如下方式訪問這個列表。 這樣的:
33.9249 18.4241 This is a message
34.9249 18.4241 This is another a message
35.9249 18.4241 This is a quick message
移動到我目前的困難,我需要給這個輸入數據,按以下格式:
var markers = [
{
coordinates:{lat:-33.9811688, lng:18.644},
message: 'Task completed at Stellenbosch Station'
},
{
coordinates:{lat:-33.9811688, lng:19.644},
message: 'Please report for duty'
}
]
我的問題,因此是如何從對象送的名單去從服務器到Java腳本中的一組json對象。
// taglib solution that I am currently trying:
Using taglib:
<%@ taglib prefix="json" uri="http://www.atg.com/taglibs/json" %>
<h1>List of Geo-Location points: </h1>
<c:forEach items="${mapcontent}" var="point">
<!-- ${point.name} ${point.telephoneNumber} ${point.genericDateTime} -->
<json:object>
<json:array name="coordinates" var="coordinate" items="${point.coordinates}">
<json:object>
<json:property name="latitude" value="${item.latitude}"/>
<json:property name="longitude" value="${item.longitude}"/>
</json:object>
</json:array>
</json:object>
<json:property name="message" value="${point.genericDateTime}"/>
</c:forEach>
Danke!
你需要一個JSON解析器,如傑克遜。順便說一句,你應該告訴我們你的_list_ objects_看起來像什麼。 –
我已更新對象列表 – Harriet