2013-07-24 29 views
0

我做了一個程序,它解析數據,然後用它做一個python json.dumps()。接下來,在我的JavaScript中,我用這個數據做了一個jQuery getJSON()。 在我用數據完成json.dumps()之前,我將它分成三個列表,因爲我不知道如何處理js中的數據。數據的結構是這樣的:參考javascript中的一個python字典的特定部分

Key: (value1, value2) 

我只需要在我的JavaScript中分別引用這些個人'列'。我覺得使用Python字典執行dumps()可能更有效,但我不知道如何在javascript中引用它。顯然重要的是數據保持「分組」

我該怎麼做呢?

+0

能否請你給你正在傾倒的數據的更詳細的例子,你想如何在javascript參考? –

+0

這聽起來像你知道該怎麼做,你只需要閱讀如何訪問JSON中的值。 –

回答

0

下面是我在地圖項目中使用的完整示例。 JavaScript從容器應用程序通過Ajax加載數據。

JQuery ajax方法與getJSON方法非常相似。

#ajax method to retreive well data for dynamic well values, x_pos, y_pos, substance concentration 
@app.route('/getWellData', methods=['GET', 'POST']) 
def getWellData(): 
    #get all samples with that date 
    date_collected = request.args.get('date_collected') 
    site_id = request.args.get('site_id') 
    site_map_id = request.args.get('site_map_id') 
    substance_id = request.args.get('substance_id') 

    well_results = wellSubstanceDataBySite(
     site_id=site_id, 
     site_map_id=site_map_id, 
     date_collected=date_collected, 
     substance_id=substance_id) 

    #return json to updateMarks ajax javascript function 
    return json.dumps(well_results) 

的Javascript:

//call the ajax endpoint for getWellData to return position, values, etc 
$.ajax({ 
    dataType: "json", 
    url: '/getWellData', 
    data: data, 
    success: function(data){ 

     //iterate over each value in the data array and append it as div element to the .landmarks div 
     $.each(data, function(well_id, well){ 

      //create the mark element, must be all mashed into one line, wont work with multiple lines 
      //sutract depth_of_water (well.value) from well.top_of_casing 
      var goundwater_elevation_val = well.top_of_casing - well.value 
      var mark = '<div class="item mark" id="well-' + well_id + '" data-id="' + well_id + '" data-position="' + well.xpos + "," + well.ypos + '" data-value="' + goundwater_elevation_val.toFixed(4) + '" data-show-at-zoom="0"><div><div class="text"><input class="well-checkboxes" type="checkbox" name="enable-well-' + well_id + '" checked style="margin:3px;"><strong>' + goundwater_elevation_val.toFixed(4) + '</strong></div><img src="/static/jquery-image-viewer/example/images/mark.png" width="50px" height="50px" alt="Permanent Mark" /></div></div>'; 
      if (well.value != 0) { 
       //append the new mark to the .landmarks div 
       $('.landmarks').append(mark); 
      } 

     }); 

     //refresh all landmarks to plot the new landmarks on the map with the smoothZoom API 
     $('#sitemap').smoothZoom('refreshAllLandmarks'); 
    } 
});