0

I have an AJAX returning JSON array from PHP.

I have the JSON array successfully returning to the AJAX request, and i am cycling through the results.

Q. How do i use the specific values from the array in this instance?

Currently i can alert the string of the results.

I wish to be able to use the values independently.

The array outputs in a string: {"btn_col_preset_id":"1","btn_col_preset_title":"Pink","btn_col_preset_bg":"#ff66ef","btn_col_preset_text":"#16f3ed"}

The js/json

for (var i in myObject) {
    if (myObject.hasOwnProperty(i)) {
     //console.log(myObject[i]);
     // alert(JSON.stringify(myObject[i])); 
        val1 = ???; // this is what i am trying to achieve
    }
}   

Updated

The full Ajax in which i am trying to get a single value based on key. This outputs empty alerts.

$.ajax({
            type: 'POST',
            url: url, 
            dataType: 'json',

            beforeSend: function() {

            },
            success: function(data) {

                var myObject = data;

                // loop over each item
                for (var i in myObject) {
                    if (myObject.hasOwnProperty(i)) {
                       //console.log(myObject[i]);
                      // alert(JSON.stringify(myObject[i]));  
                       alert(myObject["btn_col_preset_id"]);
                    }
                }   
            }
});
7
  • does console.log(data) return a string or an object? Commented Jul 19, 2014 at 12:38
  • Yes, it produces [object Object],[object Object],[object Object],[object Object],[object Object],[object Object] @andrew Commented Jul 19, 2014 at 12:42
  • ok so it sounds like you have nested obects so you could try for (var i in myObject) {for (var j in myObject[i]) {console.log(myObject[i][j]);}} Commented Jul 19, 2014 at 12:48
  • yes, thats managed to get me each key and value one after the other, is there no explicit way to grab a value by the key name though? @andrew Commented Jul 19, 2014 at 12:57
  • 1
    ok fyi myObject[i]['btn_col_preset_title'] would achieve the same Commented Jul 19, 2014 at 13:10

2 Answers 2

0

Either set

 header('Content-type: application/json');

in your php which will tell the javascript to interpret the data as JSON.

Or use

 JSON.parse();

in your javascript to convert the string to an object.

Sign up to request clarification or add additional context in comments.

Comments

0

First of all, you need to parse the JSON-encoded string then you can use it as an object in Javascript

var data = JSON.parse(result_of_the_request), i;
for(i in data)
{
     alert("The item '" + i + "' has a value of '" + data[i] + "'");
}

Note that, if you're using jQuery, there is an awesome shortcut to get the result of the resquest directly as a Javascript object. It's called $.getJSON().

2 Comments

The data is retrieved by AJAX dataType: 'json' already. The issue i have is when i try to output a single value based on the key it gives me nothing in the alert.
There are two ways of selecting a value of a JSON Object. You can either use object.<name of the key> where name of the key is hardcoded or object[i] where i is a string containing the index of the key. Using object[<name of the key>] with <name_of_the_key> hardcoded will result n the call of an undefined variable <name_of_thekey>. You will then call object[undefined] which will be undefined too.

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.