Javascript Uncaught SyntaxError: Unexpected string in JSON at position 7 when using $.parseJSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42770550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:25:40  来源:igfitidea点击:

Uncaught SyntaxError: Unexpected string in JSON at position 7 when using $.parseJSON

javascriptjqueryjson

提问by junaid afzal

I am trying to create named Object in javascript like this: -

我正在尝试在 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]
}

and so on. I am getting country, latitude, longitudevalues from database. Below is my code: -

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

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++;

}

But I am getting the error "Uncaught SyntaxError: Unexpected string in JSON at position 7". I think I am not creating the object in right way. Please help. Thanks in advance.

但我收到错误"Uncaught SyntaxError: Unexpected string in JSON at position 7"。我认为我没有以正确的方式创建对象。请帮忙。提前致谢。

Edit

编辑

Here is what I have in countries_coordarray a complete JSON: -

这是我在country_coord数组中拥有的完整 JSON:-

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

Australia
:
Object
latitude
:
"-25.274398"
longitude
:
"133.775136"

and so on I have another values in same format.

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

回答by Hogan

I miss-understood the question here is the solution

我想念这里的问题是解决方案

change

改变

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

to

objFormat = '{"id": "marker_' + i + '",'+
             '"tooltip": ' + country + '",'+
                   //etc 
             '"geoCoords": [ '+ values.latitude + ', '+values.longitude+' ]}';

Notice how I use the code indenting to make it easy to see when I have error. You had the quote after the colon.

请注意我如何使用代码缩进以便在出现错误时易于查看。您在冒号后引用了引号。

A JSON object needs to be a single object or an array of objects so you want

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]
  }
]

or

或者

{
  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]
    }
  ]
}