简体   繁体   English

将HTML中的JSON数据传递给JSON

[英]Passing JSON data in HTML to JSON

From my controller page, I set my JSON to .jsp 在控制器页面中,我将JSON设置为.jsp

jsonObject.add("map:", jsonArray);
session.setAttribute("mapJSON", gson.toJson(jsonObject));

Right now my .jsp page stores a JSON output from my controller 现在,我的.jsp页存储了我的控制器的JSON输出

<%=session.getAttribute("mapJSON")%>

From the same .jsp page, I got a script tag below trying to get the JSON data and generate some result 在同一个.jsp页面中,我在下面获得了一个脚本标签,试图获取JSON数据并生成一些结果

var colors = ['rgb(240,249,232)', 'rgb(204,235,197)', 'rgb(168,221,181)', 'rgb(123,204,196)', 'rgb(78,179,211)', 'rgb(43,140,190)', 'rgb(8,88,158)'];
var data = {};
var mapJSON = <%=session.getAttribute("mapJSON")%>;

$(document).on('click', '#map-btn', function (event) {
    $('#place').html('-');
    $('#interest-point').html('-');


    $.getJSON(mapJSON , function (results) {
        results.map.forEach(function (area, i) {
            var id = area['place'];
            var population = area['interest-point'];

            data[id] = area;

            $('#' + id).css('fill', colors[population]);
        });
    });
});

Are they any ways that I could access my JSON data from jQuery? 我是否可以通过jQuery访问JSON数据?

You can read mapJSON as String field and use JSON library for converting: 您可以将mapJSON读取为String字段,并使用JSON库进行转换:

  1. String to JSON 字符串到JSON
  2. JSON to string in jQuery JSON到jQuery中的字符串

Example Library: 示例库:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM