4

I'm working on my first (EVER!) JSON project, and I'm wondering how to get data from a JSON object. I've imported it in, and what I need to do is get the input from the value field and compare it to an input the user has entered. Unfortunately, I can't even figure out how to get data from the JSON object. It seems to be loading successfully, but I can't reference it. Here' s a sample of the JSON I'm loading:

{
    "films": [{
        "label": "34",
        "value": "34",
        "currently_streaming": "1",
        "full_streaming_url": "http://www.url.com",
        "url": "http://www.url.com"},
    {
        "label": "A Different Color",
        "value": "A Different Color",
        "currently_streaming": "1",
        "full_streaming_url": "http://www.url.php",
        "url": "http://www.url.com"}]
}​

And here's the code that loads it. It returns as successful, but I can't get any of the data from the JSON object:

 $(document).ready(function(){
    $.ajax({
        dataType: 'json',
        beforeSend : function() {
           console.log('Before Ajax Request Starts !!');
        },
        success: function(data) {
            console.log(data);
            alert("Edddddddd");
            $.each(json.films, function(i, object) {
            $.each(object, function(property, value) {
            alert(property + "=" + value);
    });
});
        },
        error : function(jqXHR, textStatus, errorThrown) {
             alert("Error occurred: " + errorThrown);
        },
         beforeSend : function() {
           console.log('Ajax Request Complete  !!');
        },
        url: 'test.php'
    });  
});

I clearly have no idea what I'm doing, and so would really appreciate any help!

2
  • Hi. First of all, do you know the JSON's syntax? By instance, working with Java/Android, you can extract the values with get methods (i.e. myjsonobject.getString("fieldname")). All JSON values are composed by its "label", a kind of string that you can reference, and its values. By example, you can get the String "34" in your sample with jsonobject (the object that holds your example) as jsonobject.getJSONArray(0).getString("label"). Based in your code, it seens that you are using it in web-services, but I'm sure that the code will not be so different! ;-) Commented Sep 21, 2012 at 17:48
  • I don't really know it - I've been reading the forums and was trying to do something along the lines of "document.writeln(employees.sales[0].firstName + ' ' + employees.sales[0].lastName); " maybe looping through that to get the values, but that wasn't working, though that may be due to my mislabeled function. I'll try that now - believe it or not, my assignment is not actually to create a bunch of alert boxes with the values. :) Commented Sep 21, 2012 at 17:51

1 Answer 1

5

Change from:

$.each(json.films, function(i, object)

to:

$.each(data.films, function(i, object)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.