how are you?
I have the following problem. at the university, they gave me a JSON file but the content of this is something particular. I have to fill a table with this JSON file I share them right away.
json:
data='[{"ID":"36","name":"Finland","population":"5300000","date":"August 21 2011","percentage":"2.05%"}]';
try to read it with the following code but it throws an error as it does not accept data = ‘[]’;
HTML JQUERY
//lee el archivo json
$.getJSON("data/countries.json",function(data){
//variable contructora de la tabla
var employee_data = '';
//obtener valores en bace a la llave
$.each(data,function(key,value){
employee_data += '<tr>';
employee_data += '<td>'+value.ID+'</td>';
employee_data += '<td><img src="imagenes/'+value.name+'" alt="'+value.name+'"></td>';
employee_data += '<td>'+value.name+'</td>';
employee_data += '<td>'+value.date+'</td>';
employee_data += '<td>'+value.percentage+'</td>';
employee_data += '</tr>';
});
//muestra el resultado de la tabla creada
$('#tab-paises').append(employee_data);
},'html').done(function(){
}).fail(function(e){
console.log("error");
console.log(e)
}).always(function(){
});
});
How could I read these types of files?
3
Answers
Thanks, I already solved it. Use part of the two codes. The final result was successful. I share the code.
Consider the following example.
You can use this to cleanup the text. Once in the correct syntax, you can parse it.
You might consider using
$.ajax()
versus$.getJSON()
in this case.The string you have is actually valid Javascript code. And it does assign a JSON string to the variable data. Thus it can be processed like this:
Or even simpler,