[jquery] loop through json array jquery

I'm trying to loop through this to get the 'name' values. This is what I currently have, but it doesn't seem to be working, tried a few others from what was posted here but nothing seemed to work.

    $.get("/get_names", {campaign_id: $('select[name="id"]').val()}, 
                function(data){
                    $.each(data, function(i, item) {
                        alert(item);
                    });
                }
   );

Json being returned:

   [
           {
              "name":"age"
           },
           {
              "name":"asdf"
           },
           {
              "name":"drivername"
           },
           {
              "name":"drivers"
           },
           {
              "name":"firstname"
           },
           {
              "name":"gender"
           },
           {
              "name":"lastname"
           },
           {
              "name":"make"
           },
           {
              "name":"model"
           },
           {
              "name":"vehicles"
           },
           {
              "name":"year"
           }
        ]

I've tried using:

item.name
item[i].name

Any suggestions?

Thank you!

This question is related to jquery json loops

The answer is


I dont think youre returning json object from server. just a string.

you need the dataType of the return object to be json


you could also change from the .get() method to the .getJSON() method, jQuery will then parse the string returned as data to a javascript object and/or array that you can then reference like any other javascript object/array.

using your code above, if you changed .get to .getJSON, you should get an alert of [object Object] for each element in the array. If you changed the alert to alert(item.name) you will get the names.


Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to json

Use NSInteger as array index Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) HTTP POST with Json on Body - Flutter/Dart Importing json file in TypeScript json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 190) Angular 5 Service to read local .json file How to import JSON File into a TypeScript file? Use Async/Await with Axios in React.js Uncaught SyntaxError: Unexpected token u in JSON at position 0 how to remove json object key and value.?

Examples related to loops

How to increment a letter N times per iteration and store in an array? Angular 2 Cannot find control with unspecified name attribute on formArrays What is the difference between i = i + 1 and i += 1 in a 'for' loop? Prime numbers between 1 to 100 in C Programming Language Python Loop: List Index Out of Range JavaScript: Difference between .forEach() and .map() Why does using from __future__ import print_function breaks Python2-style print? Creating an array from a text file in Bash Iterate through dictionary values? C# Wait until condition is true