0

I am trying to setup an analytics script in which I use json like this:

var visitors = [
    { date: "2014-01-11", value: 7 },
    { date: "2014-01-12", value: 2 },
    { date: "2014-01-13", value: 5 },
];

Now, I am getting my json through ajax from an php page like this:

jQuery.getJSON( "assets/ajax/flurry.php", { method: "ActiveUsers" } )
  .done(function( json ){

    var visitors = json; 
    console.log( "JSON Data: " + json );

});

This produces this:

JSON Data: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

My output from the php page look like this:

[{"@date":"2015-01-14","@value":"948"},{"@date":"2015-01-15","@value":"4720"},{"@date":"2015-01-16","@value":"4989"},{"@date":"2015-01-17","@value":"5221"},{"@date":"2015-01-18","@value":"5658"},{"@date":"2015-01-19","@value":"5484"},{"@date":"2015-01-20","@value":"5508"},{"@date":"2015-01-21","@value":"5560"},{"@date":"2015-01-22","@value":"5576"},{"@date":"2015-01-23","@value":"5452"},{"@date":"2015-01-24","@value":"5524"},{"@date":"2015-01-25","@value":"5804"},{"@date":"2015-01-26","@value":"5714"},{"@date":"2015-01-27","@value":"5478"},{"@date":"2015-01-28","@value":"0"}]

How do I get it to produce an javascript json like the first one?

Any help is appreciated :-)

Mansa
  • 2,277
  • 10
  • 37
  • 67
  • You need to read a bit about the difference between JSON (a string) and a Javascript literal object (a piece of Javascript). They are not the same and you can't just add two of them together. – jfriend00 Jan 28 '15 at 17:37
  • possible duplicate of [Get JSON object from AJAX call](http://stackoverflow.com/questions/22217635/get-json-object-from-ajax-call) – Andres Jan 28 '15 at 17:44

4 Answers4

4

When you use the following line:

console.log( "JSON Data: " + json );

The code casts the entire object as a string, which is usually not a very useful things as it replaces anything that is an object with this nice [object Object] you're getting.

You could either log the object itself in a separate parameter:

console.log( "JSON Data: ", json );

Or dump the JSON representation of the object:

console.log( "JSON Data: ", JSON.stringify(json));

But you'll probably see that your json variable is already what you expected.

benrict
  • 369
  • 1
  • 4
1

You need to use JSON.stringify() function

console.log( "JSON Data: " + JSON.stringify(json) );
Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
0

When you do "JSON Data: " + json, you are actually converting the json object to a string.

You can use "JSON Data: " + JSON.stringify(json) instead.

theonlygusti
  • 11,032
  • 11
  • 64
  • 119
luin
  • 2,077
  • 2
  • 20
  • 23
  • It doesn't "*convert*" the json object to a string; it *casts* it. Be careful of your wording, your current post could give the impression that the variable itself is being modified - it is not. – theonlygusti Jan 28 '15 at 17:42
0
"JSON Data: " + json

Will convert the json object into a string which looks like [object Object].

You can just do:

console.log( "JSON Data: ", json );

to write the object to the console instead of its string representation.

theonlygusti
  • 11,032
  • 11
  • 64
  • 119