简体   繁体   English

在jQuery函数中导入不带引号的json文件

[英]Importing json file inside jquery function without quotation marks

This is my hotspot.json file that I want to import inside a jquery function. 这是我想在jquery函数中导入的hotspot.json文件。 The issue i struggle with are the quotation marks on the hotspot value. 我遇到的问题是热点值上的引号。 Without them the json file is not valid but now it is not working. 没有它们,json文件将无效,但现在无法正常工作。

[
{
"pitch": 14.1,
"yaw": 1.5,
"cssClass": "custom-hotspot",
"createTooltipFunc": hotspot,
"createTooltipArgs": "Baltimore Museum of Art"
},
{
    "pitch": -9.4,
    "yaw": 222.6,
    "cssClass": "custom-hotspot",
    "createTooltipFunc": hotspot,
    "createTooltipArgs": "Art Museum Drive"
    },
    {
        "pitch": -0.9,
        "yaw": 144.4,
        "cssClass": "custom-hotspot",
        "createTooltipFunc": hotspot,
        "createTooltipArgs": "North Charles Street"
    }
    ]    

This is how i import the json file at this moment. 这是我此时导入json文件的方式。

var hotspots = (function() {
$.ajax({
    'async': false,
    'global': false,
    'url': "/hotspot.json",
    'dataType': "json",
    'success': function (data) {
        hotspots = data;
    }
});
return hotspots;
})();

At this point i don't know where to start. 在这一点上,我不知道从哪里开始。 Do i need to change something in my json file or fix the problem in the js file? 我是否需要更改json文件中的内容或解决js文件中的问题? Can someone help me to tackle this problem? 有人可以帮我解决这个问题吗?

You can first get hotspots with ajax and after use results to create your pannellum.viewer. 您可以先使用Ajax获取热点,然后再使用结果创建Pannellum.viewer。

Assuming your ajax json call return an array with this structure: 假设您的ajax json调用返回具有以下结构的数组:

[
    {
        "pitch": 14.1,
        "yaw": 1.5,
        "createTooltipFunc": hotspotDisplay1,
        "createTooltipArgs": "My hotspot 1"
    },
    {
        "pitch": -9.4,
        "yaw": 222.6,
        "createTooltipFunc": hotspotDisplay2,
        "createTooltipArgs": {
            text : "My hotspot 2"
            url : "https://your_url_2.com/"
        }
    },
    ...
]

To identify pitch and yaw, add option 'hotSpotDebug' in pannellum.viewer ( see library doc) 要确定俯仰和偏航, 在pannellum.viewer中添加选项“ hotSpotDebug”( 请参阅库文档)

Functions to handle display of tooltips: 处理工具提示的功能:

// Hot spot display creation function without link
function hotspotDisplay1(hotSpotDiv, args) {
    hotSpotDiv.classList.add('my-custom-tooltip1');
    var span = document.createElement('span');
    span.innerHTML = args + ' (my display without link)';
    hotSpotDiv.appendChild(span);
}

// Hot spot display creation function with link
function hotspotDisplay2(hotSpotDiv, args) {
    hotSpotDiv.classList.add('my-custom-tooltip2');
    var span = document.createElement('span');
    span.innerHTML = '<a href="' + args.url + '" target="_blank">' + args.text + '</a> (my display with link)';
    hotSpotDiv.appendChild(span);
}

Function to create your pannellum.viewer with result of ajax call 创建带有ajax调用结果的pannellum.viewer的函数

function createPannellumViewer(hotspotList) {
    // Create viewer
    viewer = pannellum.viewer('yourElementId', {
        ...
        //"hotSpotDebug": true, // Use to display pitch/yaw
        "hotSpots": hotspotList
    });
}

Ajax cal himself to call when document is ready 文件准备好后,Ajax会亲自致电

// Get hotspot with ajax
$.ajax({
    'url': "/hotspot.json",
    'dataType': "json",
    'success': function (data) {
        // Create pannellum.viewer
        createPannellumViewer(data);
    }
});

From the comments can be gathered that the goal is to pass a JavaScript function in the JSON data. 从注释中可以得出,目标是在JSON数据中传递JavaScript函数。 It is very much possible but I would not recommend it. 这很有可能,但我不建议这样做。 Here are 3 other questions that cover this topic: first , second and third . 以下是涉及此主题的其他3个问题: 第一第二第三

In order to make this work you would have to pass the function as a string inside the JSON (for example: "function doSomething() { alert("something");}" ) and on the receiving end you will have to parse the JSON and pass the string with code to the eval() function. 为了完成这项工作,您必须在JSON内部将函数作为字符串传递(例如: "function doSomething() { alert("something");}" ),并且在接收端,您必须解析JSON并将带有代码的字符串传递给eval()函数。

What this function does is evaluate a string at run time and runs it as code. 该函数的作用是在运行时评估字符串并将其作为代码运行。 I would not recommend this as it can be slow (especially when there is a lot of code to evaluate). 我不建议这样做,因为它可能会很慢(尤其是在要评估的代码很多时)。 On top of that, it is also very susceptible to tampering by the end user since any string will be evaluated, of course this is less of an issue when you don't use user input and only evaluate strings that come from the server. 最重要的是,由于任何字符串都会被评估,因此最终用户也极易篡改,当然,当您不使用用户输入而仅评估来自服务器的字符串时,这个问题就不那么重要了。

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

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