Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

570
Visualizações
Uncaught SyntaxError: Unexpected string in JSON at position 7 when using $.parseJSON

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

{
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, longitude values 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.

Edit

Here is what I have in countries_coord array a complete 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.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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

[ 
  {
    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]
    }
  ]
}
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda