skip to Main Content

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


  1. Chosen as BEST ANSWER

    Thanks, I already solved it. Use part of the two codes. The final result was successful. I share the code.

    $.ajax({
            url: "data/countries.json",
            dataType: "text",
            success: function(data) {
                eval(data);
                console.log(JSON.parse(data))
                data = JSON.parse(data);
                var row;
                $.each(data, function(i, r) {
                    row = $("<tr>");
                    $("<td>").html(r.ID).appendTo(row);
                    $("<td>").html($("<img>", {
                      src: "imagenes/" + r.name,
                      alt: r.name
                    })).appendTo(row);
                    $("<td>").html(r.name).appendTo(row);
                    $("<td>").html(r.date).appendTo(row);
                    $("<td>").html(r.percentage).appendTo(row);
                    $('#tab-paises').append(row);
                  });
            },
            error: function(e) {
              console.log("error");
              console.log(e)
            }
          });
    

  2. Consider the following example.

    $(function() {
      // Test Code
      var testData = "data='[{"ID":"36","name":"Finland","population":"5300000","date":"August 21 2011","percentage":"2.05%"}]';";
      var regex = /data='(.*)';/;
      var myData = JSON.parse(testData.replace(regex, "$1"));
      console.log(myData);
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    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.

      $.ajax({
        url: "data/countries.json",
        dataType: "text",
        success: function(data) {
          if (data.indexOf("data=") == 0) {
            data.replace(/data='(.*)';/, "$1");
          }
          data = JSON.parse(data);
          var row;
          $.each(data, function(i, r) {
            row = $("<tr>");
            $("<td>").html(r.ID).appendTo(row);
            $("<td>").html($("<img>", {
              src: "images/" + r.name,
              alt: r.name
            })).appendTo(row);
            $("<td>").html(r.name).appendTo(row);
            $("<td>").html(r.date).appendTo(row);
            $("<td>").html(r.percentage).appendTo(row);
            $('#tab-paises').append(row);
          });
        },
        error: function(e) {
          console.log("error");
          console.log(e)
        });
      });
    
    Login or Signup to reply.
  3. 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:

    const input = 'data='[{"ID":"36","name":"Finland","population":"5300000","date":"August 21 2011","percentage":"2.05%"}]';';
    eval(input);
    console.log(JSON.parse(data))

    Or even simpler,

    data='[{"ID":"36","name":"Finland","population":"5300000","date":"August 21 2011","percentage":"2.05%"}]';
    console.log(JSON.parse(data))
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search