2017-04-02 103 views
0

我有以下服務器端和客戶端JSON處理代碼的組合。我可以從remote url和本地.js文件加載json數據。 但是,不知何故,我無法弄清楚如何從下面的代碼片段中的serverjson對象加載數據。

var json ;  

    <#list jsons as json> 
      serverjson = ${json}; 
      console.log(serverjson); 


      $.getJSON(---have to load data from json object----).done(function (jsonData) { 
      var time = 0; var curc = 0; var prec = 0; 
      $.each(jsonData.current.timeSeries, function(i,item){ 
       //console.log(jsonData.current.timeSeries[i].beginTimeSeconds+" : "+jsonData.current.timeSeries[i].endTimeSeconds+" : "+jsonData.current.timeSeries[i].inspectedCount); 
       time = jsonData.current.timeSeries[i].beginTimeSeconds; 
       curc = jsonData.current.timeSeries[i].inspectedCount; 
       //curdata.addRows([curc]); 
       curdata.push(curc) 
       //data.addRows([[jsonData.current.timeSeries[i].beginTimeSeconds,jsonData.current.timeSeries[i].endTimeSeconds,jsonData.current.timeSeries[i].inspectedCount]]); 

      }); 
</#list> 

所以,最後,我不是過客remote url.js文件中的$.getJSON()功能,我需要通過serverjson對象。

請幫我解決這個問題。

在此先感謝。

回答

1

相反的:

$.getJSON(---have to load data from json object----).done(function (jsonData) { 
    // code 
}); 

做:

var jsonData = JSON.parse(serverjson); 
// code 
+0

謝謝,這個問題是一個愚蠢的一個。現在我意識到了。事實上,發佈後我有這個想法。我只需要將serverjson對象複製到jsonData。就這樣。 非常感謝。 – harshavmb

+0

是的,它取決於* serverjson *是否真的是JSON,或者已經是一個JavaScript對象。如果是第一個,則需要'JSON.parse',如果是第二個,則不需要轉換。但有人建議:如果變量不是字符串,請不要調用變量'jsonData'或'serverjson',因爲那時它們不是JSON。 JSON是一種文本格式。 JavaScript對象不是文本。 – trincot