使用 split 和 substring 获取有关字符串的一些信息

Get some information on a string with split and substring

我必须从字符串 filePos 中获取纬度和经度信息。 我必须将这些信息放入一个数组(称为 beaches)中,所以它必须是:

var beaches = [
                  ["45.411158", "11.906326", 1]// 1 is the index
                  ["45.411190", "11.906324", 2]// 2 is the index
              ]

我尝试这样做,但 eclipse 向我显示此错误:

"Uncaught TypeError: Cannot read property 'length' of undefined"

var filePos = "{'Data':'17/02/2015', 'Descrizione':'PROVA AAA', 'Lat':'45.411158', 'Lng':'11.906326', 'Foto':'sdjahvas'}" +
            "{'Data':'18/02/2015', 'Descrizione':'PROVA BAA', 'Lat':'45.411190', 'Lng':'11.906324', 'Foto':'asde'}";

var beaches = filePos.split("}") // Break up the original string on `", "`
.map(function(element, index){
    var temp = element.split(', ');
    lat = temp[2].substr(7, temp[2].length -2);
    lng = temp[3].substr(7, temp[3].length -2);
    return [lat, lng, index + 1];
}); 

console.log(beaches);
alert(JSON.stringify(beaches));

如果将对象字符串更改为有效 json 并解析它,则只需迭代对象即可。

var jsonString = filePos.replace(/\'/g, '"').split('}'),
    positionAry;
jsonString.pop();
positionAry = JSON.parse('[' + jsonString.join('},') + '}]');
beaches = positionAry.map(function (obj) {
    return [obj.Lat, obj.Lng];
});