0

Here's my AJAX request which works fine:

$.ajax
({
    url: 'api.php',
    type: 'GET',
    data: {search_term: data},
    dataType: 'JSON',
    success: function(data) {
        $('#div').html('<p>Constituency: ' + data + '</p>');
    },
    error: function() {
        $('#div').html('<p>Something went wrong. Please try again later.</p>');
    }
});

How would I build upon this to allow me to return nested values rather than just data? For example, how would I return constituency_name from this?

{"results": { "constituencies": [{"constituency_name": "Holborn and St Pancras", "member_name": "Frank Dobson", "member_party": "Labour", "member_biography_url": "http://www.parliament.uk/biographies/commons/Frank-Dobson/180", "member_website": "", "uri": "http://findyourmp.parliament.uk/constituencies/holborn-and-st-pancras.json" } ], "members": [] }}

Now what if there are two elements both called constituency_name as here?

{"results": { "constituencies": [{"constituency_name": "Cities of London and Westminster", "member_name": "Mark Field", "member_party": "Conservative", "member_biography_url": "http://www.parliament.uk/biographies/commons/Mark-Field/1405", "member_website": "http://www.markfieldmp.com", "uri": "http://findyourmp.parliament.uk/constituencies/cities-of-london-and-westminster.json" } , {"constituency_name": "Westminster North", "member_name": "Ms Karen Buck", "member_party": "Labour", "member_biography_url": "http://www.parliament.uk/biographies/commons/Ms-Karen-Buck/199", "member_website": "http://www.karenbuck.org.uk/", "uri": "http://findyourmp.parliament.uk/constituencies/westminster-north.json" } ], "members": [] }}

EDIT

Here's my code after trying tymeJV's response:

$.ajax
({
    url: 'api.php',
    type: 'GET',
    data: {search_term: data},
    dataType: 'JSON',
    success: function(data) {
        for (var i = 0; i < data.results.constituencies.length; i++) {
        var name = data.results.constituencies[i]["constituency_name"]; //name for this element in the array
            $('#div').html('<p>Constituency: ' + name + '</p>');
        }
    },
    error: function() {
        $('#div').html('<p>Something went wrong. Please try again later.</p>');
    }
});

I get the following error:

Uncaught TypeError: Cannot read property 'constituencies' of undefined

3 Answers 3

1

You would loop over your set and do what you need:

for (var i = 0; i < data.results.constituencies.length; i++) {
    var name = data.results.constituencies[i]["constituency_name"]; //name for this element in the array
    //do stuff
}
Sign up to request clarification or add additional context in comments.

2 Comments

What does your data look like? It's saying you have no constituencies property
Depending on the variable I give it, data looks like either of the JSON strings in my question.
0

All you need to do is to parse the data to json object like this

var Obj = JSON.parse(data);

now if you need to get constituency_name you do this

$('#div').html('<p>Constituency: ' + Obj.results.constituencies[0].constituency_name + '</p>');

Comments

0

You can use jQuery's $.each function to iterate over the list and return the constituency_name object:

$.each(data.results.constituencies, function(index, element) {
    console.log(element.constituency_name);
});

This will iterate over each constituency name and print it in the console.

Check out the jQuery .each() Documentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.