2

I am testing out some code and I have created a json file with the data.

The problem is that I'm getting "[object Object],[object Object]" in the alert. No data.

What I'm I doing wrong?

Here is the code:

<!DOCTYPE HTML>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script>

    $(document).ready(function() {
        $.getJSON("appData.json",function(results){alert(results);});
    });

</script>

</head>
<body>

</body>
</html>

and here is the content of appData.json

[{"foo":"bar"},{"foo2":"blablabla"}]

Also, The index.html file and the json file are both on my desktop and I am running it from there.

4 Answers 4

6

please try like this:

$.getJSON("appData.json", function(results) {
        $.each(results, function(index) {
            alert(results[index].foo);
        });
    });
Sign up to request clarification or add additional context in comments.

1 Comment

This will only work for simple json objects. If you need to access a complex model you should use recursion.
1

Well, you're getting an array of objects, and arrays and objects are data.

  //          v----first Object in the outer Array
alert(results[0].foo);
  //              ^----foo property of the first Object

It's just that an alert shows the default toString() values of the objects.

When you use $.getJSON, jQuery parsed the JSON text into JavaScript objects automatically. If you wanted the raw JSON, then make a $.get request instead.


If you want to iterate the Array, use a for loop, or one of the iteration methods from jQuery or the native API.

Comments

1

When alerting an object, it will say just that, [Object], and if using Firefox you could always do alert(results.toSource()); but a better option would be to start using the console (F12) instead:

$(document).ready(function() {
    $.getJSON("appData.json",function(results){
       console.log(results);
    });
});

and you can see the entire object and it's structure.

Comments

1

There is no problem there. That is letting you know there are two objects inside of the array. The reason that you are getting that is because it is the top level so you only get types back. A good way to access JSON data is through recursion.

function ContainsKeyValue( obj, key, value ){
for( all in obj )
{
    if( obj[all] != null && obj[all][key] == value ){
        return true;
    }
    if( typeof obj[all] == "object" && obj[all]!= null ){
        var found = ContainsKeyValue( obj[all], key, value );
        if( found == true ) return true;
    }
}
return false;
}

This will start from a given object inside of the graph, and recurse down any objects found. I use it like this:

var liveData = [];
for( var item in json.Items)
{
if( ContainsKeyValue( json.Items[item], "Key", "Value" ) === true )
{
    liveData.push( json.Items[item] );
}
}

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.