1

Hi there I was wondering if somebody could help me?

I have the following code. It retrieves JSON data from a php file. The Json is the following format :

{"Title":"rose","Price":1.25,"Number":15},{"Title":"daisy","Price":0.75,"Number":25},{"Title":"orchid","Price":1.15,"Number":7} 

This JSON is created using the following php code:

$shop = array();
$shop = array( array( Title => "rose", 
                  Price => 1.25,
                  Number => 15 
                ),
           array( Title => "daisy", 
                  Price => 0.75,
                  Number => 25,
                ),
           array( Title => "orchid", 
                  Price => 1.15,
                  Number => 7 
                )
         );

 echo json_encode($shop);

Whenever i try and access the data using obj.Title I get an undefined message.

 $.ajax({
        type: "GET",
        url: "data.php",
        success: jsonDo
    });

    //JSON DATA = {"Title":"rose","Price":1.25,"Number":15},{"Title":"daisy","Price":0.75,"Number":25},{"Title":"orchid","Price":1.15,"Number":7}

    function jsonDo(data) {

        var obj = jQuery.parseJSON(data);

        alert(obj.Title)

    }

I was wondering how I can access the keys in the JSON and display the data?

Thanks a million.

2
  • jQuery is probably parsing the JSON for you, so data is already a JS object. Try alert(data.Title); Commented Jul 4, 2012 at 21:28
  • When in doubt, console.log()! In your case, console.log(data). Commented Jul 4, 2012 at 21:35

5 Answers 5

2
var obj = jQuery.parseJSON('{"Title":"rose","Price":"1.25","Number":"15"}');

alert(obj.Title);

This work. Check difference in your code.

OK this is more correct:

var obj = [
    {"Title":"rose","Price":"1.25","Number":"15"},
    {"Title":"daisy","Price":"0.75","Number":"25"},
    {"Title":"orchid","Price":"1.15","Number":"7"}
    ];


 alert(obj[1].Title);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Except my JSON is {"Title":"rose","Price":1.25,"Number":15},{"Title":"daisy","Price":0.75,"Number":25},{"Title":"orchid","Price":1.15,"Number":7}
1

You have to specify that you are expecting a JSON object by informing the dataType: "JSON" parameter to the ajax() function, so you will not have to parse the data.

Comments

1

There seems to be some PHP errors in your code. This could cause php to raise a notice / warning which might break the Json output and cause javascript to raise errors when trying to parse it.

The correct jSon output should have been

[{"Title":"rose","Price":1.25,"Number":15},{"Title":"daisy","Price":0.75,"Number":25},{"Title":"orchid","Price":1.15,"Number":7}]

Since it is in an array, the JS should be: $.ajax({ type: "GET", url: "data.php", success: jsonDo });

function jsonDo(data) {
    var obj = jQuery.parseJSON(data);
    alert(obj[0].Title)

}

Comments

0

You should just use jQuery's $.getJSON method:

$.getJSON('data.php',function(data) {
    alert(obj.Title);
});

Comments

0

try obj[0]["Title"] or obj[0].Title

dont forget that you have nested a lot of arrays and that yoyu need to access them that way again.

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.