Result: [object], [object] when importing a JSON file

1

I have a problem getting data from a JSON file, when I try to get the data from this file, I get as a result: [object], [object]

{
  "categories": [
    {
      "categori_id": 1,
      "name": "drinks"
    },
    {
      "categori_id": 2,
      "name": "lunch"
    },
    {
      "categori_id": 3,
      "name": "food"
    },
    {
      "categori_id": 4,
      "name": "sea"
    }
  ],
  "products": [
    {
      "id": 1,
      "name": "Lorem",
      "price": "60.000",
      "available": true,
      "best_seller": true,
      "categories": [
        1,
        4
      ],
      "img": "",
      "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas eu."
    },
    {
      "id": 2,
      "name": "ipsum",
      "price": "20.000",
      "available": false,
      "best_seller": false,
      "categories": [
        4
      ],
      "img": "",
      "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas eu."
    },
    {
      "id": 3,
      "name": "dolor",
      "price": "10.000",
      "available": true,
      "best_seller": true,
      "categories": [
        4
      ],
      "img": "",
      "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas eu."
    },
    {
      "id": 4,
      "name": "sit",
      "price": "35.000",
      "available": false,
      "best_seller": false,
      "categories": [
        1,
        2
      ],
      "img": "",
      "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas eu."
    },
    {
      "id": 5,
      "name": "amet",
      "price": "12.000",
      "available": true,
      "best_seller": true,
      "categories": [
        1,
        4
      ],
      "img": "http://lorempixel.com/200/100/food/",
      "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas eu."
    },
    {
      "id": 6,
      "name": "consectetur",
      "price": "120.000",
      "available": true,
      "best_seller": false,
      "categories": [
        1,
        4
      ],
      "img": "",
      "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas eu."
    },
    {
      "id": 7,
      "name": "adipiscing",
      "price": "50.000",
      "available": false,
      "best_seller": false,
      "categories": [
        1,
        3
      ],
      "img": "",
      "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas eu."
    },
    {
      "id": 8,
      "name": "elit",
      "price": "2000",
      "available": true,
      "best_seller": false,
      "categories": [
        1,
        3
      ],
      "img": "",
      "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas eu."
    },
    {
      "id": 9,
      "name": "Maecenas",
      "price": "150.000",
      "available": true,
      "best_seller": true,
      "categories": [
        2,
        4
      ],
      "img": "",
      "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas eu."
    },
    {
      "id": 10,
      "name": "eu",
      "price": "200.000",
      "available": false,
      "best_seller": true,
      "categories": [
        2,
        3
      ],
      "img": "",
      "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas eu."
    }
  ]
}

JavaScript code:

$(document).ready(function(){

  $.ajax({
    url: "category.json", // path to file
    dataType: 'json', // type of file (text, json, xml, etc)
     success: function(data) {
     /$.each(data, function(i, item) {
          console.log(item);

      });/

     jQuery.each( data, function( i, val ) {
  $( "body" ).append( document.createTextNode( " - " + val ) );
});
    },
    error: function() {
      alert("error");
    }
  });     

});

Please, I would like to know what is happening.

    
asked by Michelle Estrada Iglesias 19.10.2016 в 14:47
source

5 answers

1

Good, you should "enter" the JSON. That is, instead of console.log(item) pon console.log(item.categories);

To keep going through the array, you should do a for, where i = 0 and even item.categories.length.

console.log(item.categories[i]);

    
answered by 19.10.2016 в 14:58
1

If I do not see the code correctly, I think you should go back to what's inside the first for each.

$.ajax({
   url: "category.json", // path to file
   dataType: 'json', // type of file (text, json, xml, etc)
    success: function(data) {
    $.each(data, function(i, item) {
         console.log(item);

        jQuery.each( item, function( i, val ) {
             $( "body" ).append( document.createTextNode( " - " + val ) );
        }); 

     });


   },
   error: function() {
     alert("error");
   }
 });     

});
    
answered by 19.10.2016 в 22:40
1

To print an attribute in specific, you must declare what that attribute is in the function each () since they are arrays and then traverse that array within another each () as follows:

$.each(data.products, function() {
   $.each(this, function(key, value) {
      console.log(key + " - " + value)
   })
})

I hope it serves you!

    
answered by 22.10.2016 в 02:02
0

Check that your server is sending the data in application/json instead of text/plain . Your jquery function is waiting for the data in json format when putting:

dataType: 'json'

On the other hand, the [object][object] is the way in which the data you are receiving is serialized. If you want to see them in the form of a tree, send them to the browser console with console.log(datos) .

    
answered by 19.10.2016 в 14:57
0

I do not really understand what you need, but you have to be clear that your JSON is composed of 2 fixes, therefore 1 each is not enough to get all the data you want.

$.each(data.categories, function(index,value){
    console.log(value.categori_id + " " + value.name);
});
$.each(data.products, function(index,value){
    console.log(value.id + " " + value.name + " " + value.price);
});
    
answered by 19.10.2016 в 15:41