2017-08-25 51 views
-1

我正在編寫一個使用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!

+0

你需要一個JSON解析器,如傑克遜。順便說一句,你應該告訴我們你的_list_ objects_看起來像什麼。 –

+0

我已更新對象列表 – Harriet

回答

0
+0

Hi @Alex - 我已經看過鏈接頁面上提供的json-taglib示例。我已經提出了一些代碼(請參閱原始發佈問題),但我不知道如何從那裏繼續。什麼是包含json結構的對象,我可以用它初始化我的標記嗎? – Harriet

+0

如果我沒有弄錯,的結果將是一個json格式的簡單字符串 –

+0

我是否正確地將代碼塊放入現有的forEach循環中?這是必要的。 – Harriet

相關問題