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

79
Vistas
How to loop through JSON object with an array with backslashes inside in Jquery/Javascript from AJAX call

I have an array where I want to loop through the array inside the JSON and show the data in the table based on the nationalities in the array. The problem is that the array data contains backslashes and I can't loop through it. I retrieve the data with an AJAX call from my PHP file.

PHP:

$arr 

if (!empty($arr)) {
        echo json_encode($arr);
      // print_r($arr);
    } else{
        $errors = 'No data available.';
    }
            exit;

If I print_r() I see: Array ( [0] => stdClass Object ( [company] => John Comp [pro_countries] => ["BR","ES","FR"])

If I use echo json_encode my echo looks like but I can print company in my Jquery: [{"company":"John Comp","nationality":"[\"BR\",\"ES\",\"FR\"]"}]

But if I use print_r($arr) the slashes disappear but I can't seem to print the company in Jquery: [{"company":"John Comp","nationality":"["BR","ES","FR"]"}]

If I use Print_r() Then my AJAX call in JQuery goes straight to the error response. JQuery:

 $.ajax({
 type: 'GET',
            url: 'testurl',
            dataType: 'json',
            processData: false,
            cache: false,
            success: function (response){
            

             response.nationality.forEach((item)=>{

            });

I also tried:

$.ajax({
 type: 'GET',
            url: 'testurl',
            dataType: 'json',
            processData: false,
            cache: false,
            success: function (response){

var data = JSON.parse(response);

data.nationality.forEach((item)=>{

  });

What I want after the loop:

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

nationality is a JSON string, you need to parse it so you can loop over it.

JSON.parse(response.nationality).forEach(item => ...)

I'm not sure why you encoded nationality in the first place. You should just make it an ordinary PHP array, and then it will be encoded as part of $arr.

about 4 years ago · Juan Pablo Isaza 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