Chris
Chris

Reputation: 18876

json comes in as object not string

Json is sitting on my localhost as: /data/:

{
"children": [
    {
        "name": "analytics",
        "size": "1243"
    },
    {
        "name": "math",
        "size": "4343"
    },
    {
        "name": "algebra",
        "size": "1936"
    },
    {
        "name": "calc",
        "size": "3936"
    },
    {
        "name": "geom",
        "size": "2136"
    },
    {
        "name": "Quant",
        "size": "4136"
    }
]

}

Here is how I am trying to access the json:

var interval = setInterval(function() {
        $.getJSON("http://localhost:8080/dev_tests/d3/examples/data/flare2.json", function(json) {
            $.each(json.children,function(i,name){
            alert(json.children);
            });

        });
        }, 3000);

The data comes in just fine. That is, when I run console.log(json) I can see the above json name/value txt pairs in firebug. However, before each name value pair I see the word Object. So for example instead of my log showing { name="analytics", size="1243"},.. it actually shows: [Object { name="analytics", size="1243"},... And that too is what my alert shows: [object Object] instead of name="analytics", size="1243". Why is this and is there a way to get my json name/value pairs in text so that I can store as a javascript String?

Many thanks in advance.

Upvotes: 0

Views: 789

Answers (4)

Christoph
Christoph

Reputation: 51181

Well, working with Objects is the intend of "Javascript Object Notation".

Actually, your json is an Array of Objects.

{
  "children": [ // <-- Array
          {...},// <- Object
          {...} // <- Object
  ]
}

You can access your key-value pairs this way: children[0].name

for (var i=0;i< children.length; ++){
    console.log( children[i].name);
    console.log( children[i].size);
}

Upvotes: 0

charlietfl
charlietfl

Reputation: 171679

Everything you've stated is normal behavior for both firebug and alert. You can't alert an object.

Here's example of how to loop the response within your $.get callback

http://jsfiddle.net/Y3N4S/

$.each(json.children, function(i, item){
    $('body').append('<p>Name: '+item.name+' , Size: '+item.size+'</p>')

})

Upvotes: 0

tvanfosson
tvanfosson

Reputation: 532435

The O in JSON stands for "object." It's a way of serializing a JavaScript object to a string (and back). You seem to be relying on the conversion on one hand (referencing children) but not want to have the conversion done on the other hand. If you really want children to be a collection of strings in the format you describe, you should store it that way. If you really do want the object notation (and conversion to objects in the client), then you can simply use the properties of the object.

var interval = setInterval(function() {
    $.getJSON("http://localhost:8080/dev_tests/d3/examples/data/flare2.json", function(json) {
        $.each(json.children,function(i,item){
           alert("name = " + item.name + ", size = " + item.size);
        });

    });
    }, 3000);

Upvotes: 1

Gumbo
Gumbo

Reputation: 655169

jQuery does automatically decode the response when using jQuery.getJSON or specifying the response as JSON. If you want the plain response, use jQuery.ajax instead.

Upvotes: 2

Related Questions