0

I have an array like this comming from php file with json_encode.

[{"label":"Mississippi State, Mississippi","value":"mississ"},
 {"label":"Mississauga, Ontario","value":"mississ"},
 {"label":"Mississippi Station, Ontario","value":"mississ"}]

This array is dynamic. I want to alert the label of this array.

JQUERY:

var obj = $.parseJSON(data);
alert(obj.label);

It's giving me error like obj is null.

EDIT:

basically I want to put these values in dropdown search box: like this jquery plugin

AJAX:

$.ajax({
            url: "ajax/ir_populate_search.php",
            dataType: "json",
            type: "POST",
            data: {
                keyword: request.term,
                path: path
            },

 success: function(data){
    var obj = $.parseJSON(data);
    var availableCities = obj.label;
    $( "#txtLocation" ).autocomplete({
                    source: availableCities
                });
}
5
  • can you give your ajax call code cause if you don't use dataType:json then your data don't have the response string. its in data.responseText. Try $.parseJSON(data.responseText); Commented Mar 26, 2014 at 17:06
  • check my edit.. I am using json dataType.. Commented Mar 26, 2014 at 17:07
  • Why are you doing parseJson of a json object ? i mean data is already the json. Commented Mar 26, 2014 at 17:10
  • dataType: "json" tells jQuery to parse it for you. data is already parsed. data is an array of objects. Commented Mar 26, 2014 at 17:11
  • you do not need to parse json its already in parsed format .. call directly like data.label Commented Mar 26, 2014 at 17:16

5 Answers 5

1

You do not need to parse json, it is already json. Use this;

$( "#txtLocation" ).autocomplete({
    source: function( request, response ) {
        $.ajax({
            url: "ajax/ir_populate_search.php",
            data: {
                    keyword: request.term,
                    path: path
            },
            dataType: "json",
            success: function( data ) {
                response( $.map( data, function( item ) {
                    return {
                        label: item.label,
                        value: item.value
                    }
                }));
            }
        });
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

It works for me.. Just one problem here. I can not select the search results. It should populate in text box after selecting.
1

From the looks of it, you already have a structure that you can work with, but it is not an Object . . . it's an array of Objects, so, to access the label parameter, you need to identify which object you want to look at, by providing its index, like this:

data[n].label

Using your example, you would get these values, for the various indexes:

  • data[0].label ==> Mississippi State, Mississippi
  • data[1].label ==> Mississauga, Ontario
  • data[2].label ==> Mississippi Station, Ontario

So, to loop through them all and get the data for each entry, you could do something like this:

for (i = 0; i < data.length; i++) {
    . . . do something with: data[i].label . . .
    . . . do something with: data[i].value . . .
}

Comments

0

If you do this dataType: "json", jQuery does JSON parsing for you, so data is already a JavaScript object. Remove var obj = $.parseJSON(data); and work with data directly.

Comments

0

Data is already the json object $.parseJSON is used to parse the JSON string to an object Like this:

$.parseJSON('[{"label":"Mississippi State, Mississippi","value":"mississ"},{"label":"Mississauga, Ontario","value":"mississ"},{"label":"Mississippi Station, Ontario","value":"mississ"}]')

Just using data and adding to the array the values you're using will work

$.ajax({
            url: "ajax/ir_populate_search.php",
            dataType: "json",
            type: "POST",
            data: {
                keyword: request.term,
                path: path
            },success: function(data){
           //data is already the array of objects
           //just loop them 

            var availableCities = [];
            for(i in data){
              availableCities.push(data[i].label)
            }


           $( "#txtLocation" ).autocomplete({
                    source: availableCities
                });
}
})

Comments

0

You don't need jquery for using a json object in javascript

Try something like that:

$data = [{"label":"Mississippi State, Mississippi","value":"mississ"},
         {"label":"Mississauga, Ontario","value":"mississ"},
         {"label":"Mississippi Station, Ontario","value":"mississ"}]

$data[0].label

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.