Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

316
Views
Cómo convertir Getjson a valores en un diccionario o matriz en Javascript

Tengo una función Javascript y obtengo los siguientes valores en la consola si ejecuto los códigos:

 $(document).ready(function(){ $.getJSON('http://example.com/json.php',function(urldata){ console.log(urldata); }); })

Obtengo lo siguiente en la consola:

 {data: Array(3), draw: 1, recordsTotal: 3, recordsFiltered: 30} data: Array(3) 0: (6) [1, 'Tokyo', 'Japan', '83', '9', 0] 1: (6) [2, 'Paris', 'France', '16', '8', 0] 2: (6) [3, 'Rome', 'Italy', '44', '6', 0] length: 3 [[Prototype]]: Array(0)

Traté de ponerlos en una matriz pero recibí un mensaje de error indefinido.

 var arr = []; $(document).ready(function(){ $.getJSON('http://example.com/json.php',function(urldata){ for(k in urldata) { alert("City: " + k[1] + " Country: " + k[2]); arr.push(k[1], k[2]) } }); })

Me gustaría tener el siguiente resultado en una matriz o diccionario con fácil acceso a los valores:

 +---+-------+--------+ | 1 | Tokyo | Japan | +---+-------+--------+ | 2 | Paris | France | +---+-------+--------+ | 3 | Rome | Italy | +---+-------+--------+

Por favor, ¿qué está mal con el código?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Según la estructura de la respuesta, intente reemplazar su devolución de llamada a arr.push(urldata.data)

 let arr = []; $(document).ready(function(){ $.getJSON('http://example.com/json.php',function(urldata){ arr.push(urldata.data) // console.log(urldata) }); })

Actualizado

Según la respuesta proporcionada en su console.log ,

 data: Array(3) 0: (6) [1, 'Tokyo', 'Japan', '83', '9', 0] 1: (6) [2, 'Paris', 'France', '16', '8', 0] 2: (6) [3, 'Rome', 'Italy', '44', '6', 0]

Puede extraer los datos utilizando los siguientes ajustes. Tenga en cuenta que solo usé for loop por simplicidad y para que pueda verlo, pero hay métodos más limpios para esto.

 let arr = []; $(document).ready(function(){ $.getJSON('http://example.com/json.php',function(urldata) { for (let i=0; i<urldata.data.length; i++) { let formattedData = `City: ${urldata.data[i][1]} Country: ${urldata.data[i][2]}` } }); })
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!