Uncaught SyntaxError:使用$ .parseJSON时,JSON在位置7处出现意外字符串

jun*_*zal 2 javascript jquery json

我正在尝试在javascript中创建命名对象,如下所示:-

{
id: "marker_0",
tooltip: country,
src: "//localhost/mapsvg/markers/pin1_red.png",
width: 15,
height: 24,
geoCoords: [latitude, longitude]
},
{
id: "marker_1",
tooltip: country,
src: "//localhost/mapsvg/markers/pin1_red.png",
width: 15,
height: 24,
geoCoords: [latitude, longitude]
}
Run Code Online (Sandbox Code Playgroud)

等等。我从数据库中获取国家,纬度,经度值。下面是我的代码:-

var objFormat;
var i =0;
var mapFormat = [];
for (var country in countries_coord) {
  values = countries_coord[country];
  objFormat = '{"id:" "marker_""' + i + '","tooltip:" "' + country + '","src:" "//localhost/mapsvg/markers/pin1_red.png","width:" "'+ 15 + '","height:" "'+ 24 + '","geoCoords:" [ "'+ values.latitude + '", "'+values.longitude + '" ]}';
  obj = $.parseJSON(objFormat);
  mapFormat.push(objFormat);
  i++;

}
Run Code Online (Sandbox Code Playgroud)

但我收到错误消息“ Uncaught SyntaxError:JSON中位置7处的意外字符串”。我认为我没有以正确的方式创建对象。请帮忙。提前致谢。

编辑

这是我在countries_coord数组中具有完整JSON的内容:-

Afghanistan
:
Object
latitude
:
"33.93911"
longitude
:
"67.709953"

Australia
:
Object
latitude
:
"-25.274398"
longitude
:
"133.775136"
Run Code Online (Sandbox Code Playgroud)

依此类推,我还有另一个格式相同的值。

Hog*_*gan 5

我错过了一个问题,那就是解决方案

更改

  objFormat = '{"id:" "marker_""' + i + '","tooltip:" "' + country + '","src:" "//localhost/mapsvg/markers/pin1_red.png","width:" "'+ 15 + '","height:" "'+ 24 + '","geoCoords:" [ "'+ values.latitude + '", "'+values.longitude + '" ]}';
Run Code Online (Sandbox Code Playgroud)

objFormat = '{"id": "marker_' + i + '",'+
             '"tooltip": ' + country + '",'+
                   //etc 
             '"geoCoords": [ '+ values.latitude + ', '+values.longitude+' ]}';
Run Code Online (Sandbox Code Playgroud)

请注意,我如何使用代码缩进来使我在遇到错误时更容易看到。您在冒号后面加上了报价。

JSON对象必须是单个对象或对象数组,因此您需要

[ 
  {
    id: "marker_0",
    tooltip: country,
    src: "//localhost/mapsvg/markers/pin1_red.png",
    width: 15,
    height: 24,
    geoCoords: [latitude, longitude]
  },
  {
    id: "marker_1",
    tooltip: country,
    src: "//localhost/mapsvg/markers/pin1_red.png",
    width: 15,
    height: 24,
    geoCoords: [latitude, longitude]
  }
]
Run Code Online (Sandbox Code Playgroud)

要么

{
  location_list: [
    {
      id: "marker_0",
      tooltip: country,
      src: "//localhost/mapsvg/markers/pin1_red.png",
      width: 15,
      height: 24,
      geoCoords: [latitude, longitude]
    },
    {
      id: "marker_1",
      tooltip: country,
      src: "//localhost/mapsvg/markers/pin1_red.png",
      width: 15,
      height: 24,
      geoCoords: [latitude, longitude]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)