Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

174
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.

8 months 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]
    }
  ]
}
8 months 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 empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.