在firefox控制台中抛出SyntaxError: JSON.parse: unexpected character error的空字符串的解决方法是什么?

What is a workaround for empty string throwing a SyntaxError: JSON.parse: unexpected character error in firefox console?

提问人:Toyu_Frey 提问时间:10/27/2023 更新时间:10/27/2023 访问量:20

问:

我有一个 JSON 文件,其键值对有 4164 行空值。当我尝试将所有键值对的值输出到 Firefox 控制台时,我收到以下错误消息。

未捕获(在 promise 中) SyntaxError:JSON.parse:JSON 数据第 1 行第 1 列处出现意外字符

研究错误消息使我得到了这个答案。不幸的是,它没有包括该问题的解决方法。

let myMap = L.map("map", {   center: [40.7, -73.95],   zoom: 11 });

//Adding the tile layer 
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {   attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' }).addTo(myMap);


let link = 'https://geojson.io/#map=4.08/39.3/-97.79'

  // Getting our GeoJSON data 
d3.json(link).then(function (data) {
    // Creating a GeoJSON layer with the retrieved data
    L.geoJson(data, {
        style: function (feature) {
            return {
                color: "white",
                fillColor: chooseColor(feature.Malden.RegionName),
                fillOpacity: 0.5,
                weight: 1.5
            }
        },
        onEachFeature: function (feature, layer) {
            layer.on({
                mouseover: function (event) {
                    layer = event.target;
                    layer.setStyle({
                        fillOpacity: 0.9
                    })
                },
                mouseout: function (event) {
                    layer = event.target;
                    layer.setStyle({
                        fillOpacity: 0.5
                    })
                },
                click: function (event) {
                    myMap.fitBounds(event.target.getBounds());
                }
            });
            layer.bindPopup(
                "<h1>" + feature.Malden.RegionName + "</h1><h2> " + feature.Malden.RegionName + "</ > "
            );
        }
    }).addTo(myMap); });

  d3.json("./data/trueData.json").then(function (data) {

let regionNameVar2;
let regionLatVar2;
let regionLngVar2;
let countyLatVar2;
let countyLngVar2;
let countyNameVar;
let lngDifference;
let latDifference;
let countyAbs;

let regionCordinates2 = [];
let regionLatArray = [];   
let regionLngArray = [];

    for (let x in data) {

    // attempt to grab the values using square bracket notation
     regionNameVar2 = data[x]["RegionName"];
     regionLatVar2 = data[x]["RegionLat"];
     regionLngVar2 = data[x]["RegionLng"];
     countyLatVar2 = data[x]["CountyLat"];
     countyLngVar2 = data[x]["CountyLng"];
     countyNameVar = data[x]["CountyName"]
      // outputs both region latitude and region longitude to make region cordinates
      regionCordinates2 = [regionLatVar2, regionLngVar2];
     

     for(let n in data){
      lngDifference = regionLngArray - countyAbs

      function chooseColor(lngDifference){
        if (lngDifference < 1) return "Yellow";
        else if (lngDifference < 0.5) return "red";
        else if (lngDifference < 0.05) return "orange";
        else if (lngDifference < 0.005) return "green";
        else if (lngDifference < 0.0025) return "purple";
        else return "black";
      }

    }

//finds the absolute value of region longitude cordinates for further manipulation,
//stores it inside of a array
 regionLngArray = Math.abs(regionLngVar2);
 regionLatArray = Math.abs(regionLatVar2);

 //gets the absolute value of county latitude values
 countyAbs = Math.abs(countyLngVar2)

     }// for (let x in data) end bracket
 }
)
json string firefox geojson is-empty

评论

0赞 Kaiido 10/27/2023
指向 (geojson.io/#map=4.08/39.3/-97.79) 的地址不是 JSON 文件,而是 html 文件link
0赞 Toyu_Frey 10/27/2023
@Kaiido啊。所以如果我重复 d3.json(“./data/trueData.json”).then(function (data) { 那么它应该可以工作吗?
0赞 Kaiido 10/27/2023
我不知道,但正如它所写的那样,知道这是正常的,它不起作用。
0赞 Toyu_Frey 10/27/2023
@Kaiido 好消息是,将链接替换为 trueData.json 消除了该错误。现在我遇到了一个不同的错误,阻止我的代码运行。新错误未捕获(在 promise 中) 错误:未找到 404

答: 暂无答案