Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

569
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda