Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am receiving this kind of JSON object (looked up through Chrome Dev Tools): JSON Array

How can I simply assign (or convert) it to the var? I mean, I would like it to become: var arr = [10, 11, 12, 13, 14, 15]

More info - this JSON object comes as AJAX response from the servlet:

EDIT:

This is how it looks now:

$(document).ready(function(){
            var rrr = [];

            $.ajax({
               url: 'DataProvider',
               type: "get",
               dataType: 'json',
               success: function(response) {
                   rrr = response;
                   console.log(+rrr); //output: [10, 11, 12, 13, 14, 15]
               }
            });

            console.log(rrr); //output: []   - why is the array empty here???

            var ctx = $('#myChart').get(0).getContext("2d");

            var data = {
                labels : ["Roll 1","Roll 2","Roll 3","Roll 4","Roll 5","Roll 6"],
                datasets : [
                    {
                        fillColor : "rgba(210,220,220,0.5)",
                        strokeColor : "rgba(220,220,220,1)",
                        data : rrr
                    }
                ]
            }
            var myNewChart = new Chart(ctx).Bar(data);
        });
share|improve this question
2  
It is an array already. What "this won't work" means? –  zerkms Mar 18 '14 at 19:52
    
What happens when you try and assign it? Do you get an error message? What makes you think it doesn't work? –  Matt Burland Mar 18 '14 at 19:52
    
The above should work fine, why doesnt it –  tymeJV Mar 18 '14 at 19:52
1  
Are you receiving any errors? Does the Ajax request itself succeed? And, where are you trying to use arr? –  Jonathan Lonowski Mar 18 '14 at 19:52
    
look this [link] (developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…) –  Igoel Mar 18 '14 at 19:53

5 Answers 5

up vote 3 down vote accepted

I'm going to make a guess that you're trying to use the date before it's ready. What you might try is using a callback to pass the data back to a point in your program so you can use it.

function getData(callback) {
  $.ajax({
   url: 'DataProvider',
   type: "get",
   dataType: 'json',
   success: function(response) {
     callback(response);
   }
  });
}

getData(function (data) {
  Chart.init(data); // or whatever you need to do with the data
});

Since $.ajax uses the promise interface, you can also do this:

function getData() {
  return $.ajax({
   url: 'DataProvider',
   type: "get",
   dataType: 'json'
  });
}

getData().then(function (data) {
  Chart.init(data); // or whatever you need to do with the data
});
share|improve this answer
$(document).ready(function(){
    var rrr = [];
    $.ajax({
           url: 'DataProvider',
           type: "get",
           dataType: 'json',
           success: function(response) {
               rrr = response;
               console.log(+rrr); //output: [10, 11, 12, 13, 14, 15]
           }
        });

        console.log(rrr); //output: [] - why is the array empty here???
        //Because the above function didn't get executed yet.
        //(code snipped)
    });
}

Do this:

$.ajax({
   url: 'DataProvider',
   type: "get",
   dataType: 'json',
   success: function(response) {
       rrr = response;
       console.log(+rrr); //output: [10, 11, 12, 13, 14, 15]
       itWorkedSoYouCanNowDoYouStuffNow();
   }
});
function itWorkedSoYouCanNowDoYouStuffNow() {
    console.log(rrr); //output: [10, 11, 12, 13, 14, 15]
}
share|improve this answer

It seems like in this case you may be better off using jQuery.getJSON(), as this can be parsed appropriately with a JSON object, like saj suggested.

http://api.jquery.com/jquery.getjson/

share|improve this answer

Thanks to Andy - I found the resolution.

Just simply add:

async: false,

To the $.ajax properties.

share|improve this answer
2  
Why do you want to delay all of JavaScript when a request is going thru? Async callbacks are a powerful feature of JS. –  bjb568 Mar 18 '14 at 20:15
1  
See my answer below, and sorry for being vague in that earlier comment. async: false is the wrong way to head imo. –  Andy Mar 18 '14 at 20:16

You need to parse the json using JSON.parse

var parsedJSON = JSON.parse(response)
share|improve this answer
    
jQuery should already perform this with dataType: 'json' being set. –  Jonathan Lonowski Mar 18 '14 at 19:54
    
$.ajax automatically returns an object if the dataType is 'json' assuming no errors. –  Andy Mar 18 '14 at 19:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.